BCA, B.Tech_info

Easy way to learn

Monday, June 4, 2018

Variable in C/C++

June 04, 2018 0
Variable in C/C++
Definition:
It is a data name which is used to store data and may change during execution. A variable is  used to create and locate the data at  a particular location in computer's memory. Variable is data item whose value can be changed during the exam execution.

syntax:



  data_type variable_name;



Rules of variable name:



  • Variable must start with a letter.
  • Variable name can contain letters, numbers and the underscore.
  • Keywords are not allowed to use as a variable name.
  • Variable name can not have space in between.
  • Variable name can not have special character.

Scope of variable:


A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all the functions. While a local variable is one declared within the body of a function or a block. 



Declaration of variable:


C++ allows the declaration of a variable can be declared right at the place of its first use. This make the program much easier to write.



Example program:



Functions

June 04, 2018 0
Functions

Functions:


A function is a self-contained block of code that programs a particular task i.e., a function groups a number of program statement into a single unit and gives it a name. This unit can be called from other parts of a program.


syntax of function:


  return_type function_name(parameter_list)

 {
    local variable declaration;
    statement;
    return(expression);
}

Example:


  int cube(int a)

  {
    int compute;
    compute=a*a*a;
    return(compute);
  }


The main() function:


C does not specify any return type for the main() function, which is the starting point for the execution of a program. There is necessary to specify atleast  one main() function in the program.



main()

{
    // statements of program
}


Function prototyping:


Function prototyping is the major improvement added to c++ function. The prototyping describes the function interface to the compiler by giving details such as member and type of arguments and the type of return values. With function prototyping, a template is always used when declaring and defining a function.When a function is called, the compiler uses the template to ensure that proper arguments passed, and the return value is treated correctly.

Function prototype is a declaration statement in the calling program.

syntax:


return_type function_name(type arg1, type arg2... );


e.g.,


float volume(int, int, float);


Different styles of writing prototypes:

  • Function with no arguments and no return value
  • Function with arguments but no return value
  • Function with no arguments but returns a value
  • Function with arguments and returns a value

Function with no arguments and no return value



When a function has no arguments, it does not receive any data from the calling function. Similarly, when it does not return any value, the calling function does not receive any data from the called function. There is no data transfer between the calling function and the called function.


syntax:


  void function();



Function with  arguments but no return value


When a function has arguments, it receive data from the calling function. Since it does not return a value, the calling function does not receive any data from the called function. The arguments in calling functions are called actual arguments whereas is called function is called formal arguments.


syntax:


void function(int );



Function with no arguments but returns a value


When a function has no argument, it does not receive any data from calling function but return back some value which may be required for processing.

syntax:

int function();



Function with arguments returns value


In the case, the function receive data from the calling function through arguments and returns back some value which may be required for further processing.


syntax:


int function(int x);



Uses of function:

  • Functions are very much useful when a block of statement has to be written/executed again and again. 
  • Problem are broken down into small and separate functions. It makes the program easier in writing and debugging.
  • Functions are also used to reduce the difficulties during debugging a program.
  • Functions help programmers to write more organized code.



Inline function:


One of the objectives of using functions in a program is to save some memory space, which becomes appreciable when a function is likely to be called many times. To eliminate the cost of calls to small functions, C++ purposes a new feature called inline function. An inline function is a function that is expended in line when it is invoked. The compiler replaces the function call with corresponding function code.


syntax:


  inline function_header

  {
       //function body
  }

Example:



#include<iostream.h>
using namespace std;

inline float mul(float x, float y)
{
   return(x*y);
}
 inline double div(double p, double q)
{
   return(p/q);
}
int main()
{
   float a=12.34;
   float b=9.82;
   cout<<mul(a,b)<<"\n";
   cout<<div(a,b)<<"\n";
   return 0;
}


Advantages of inline function



  • It saves the execution time.
  • Program becomes more readable.
  • Function call overheads are eliminated.
  • Program executes more efficiently.



Function overloading:


Overloading in C++ refers to the use of the same thing for different purposes. This means that we can use the same function name to create functions that perform a variety of different tasks. This is also known as polymorphism. Using the concepts of function overloading, we can design a family of functions with one function name but with different argument list. 


// Declarations                                              

int div(int x, int y);                                                  //prototype 1
int mul(int x, int y, int z);                                        //prototype 2
float sub(float a, float b);                                        //prototype 3

// Function calls

cout << div(3,4);                                                     //uses prototype 1
cout << mul(2, 3, 4);                                               //uses prototype 2
cout << sub(10.2, 12.5);                                          //uses prototype 3

The function would perform different operation depending upon the argument list in the function call. A function call first matches the prototype having the same number and type of arguments and then calls the appropriate function for execution. A best match must be unique.



Example program



#include<iostream.h>
using namespace std;

int vol(int);
float vol(float, int);

int main()
{
   cout << vol(5) <<"\n";
   cout << vol(2.5, 8) <<"\n";
   return 0;
}
int vol(int s)
{
   return(s*s*s);
}
float vol(float r, int h);
{
   return(3.14*r*r*h);
}

OUTPUT:

125

157.26


Friend function:


In object oriented programming, the private members cannot be accessed by the class. That is, non-member function cannot have the access of the private data of a class. However, there could be the situation where we would like two classes to share a particular function. C++ allows the common function to be made friendly with both the classes, thereby allowing the function to have access to the private data of these classes. Such a function need not be a member of any of these classes.


syntax:


class XYZ

{
   private:
      .....
      .....
   public:
      .....
      .....
      friend void abc(void);                   //declaration
}

The function declaration should be preceded by the keyword friend. The function is defined elsewhere in the program. The function keyword neither use friend keyword nor use scope resolution operator. The functions that are declared by using friend keyword are known as friend function.A friend function has full rights to access the private member of class, although it is not a member function of that class.


Example program






#include<iostream.h>
using namespace std;
class distance
{
   private:
      int meter;
   public:
      Distance():meter(0){}
      friend int fun(distance);
};
int fun(distance a)
{
   d.meter=20;
   return d.meter;
}
int main()
{
   distance a;
   cout << "Distance:"fun(a)<<"\n";
   return 0;
}


OUTPUT:

Distance:20

Polymorphism in C++