Thursday 23 May 2013

Functions in C++


About main function
l  In C++, default return type of main function is int.
l  In C++, if a main function is not prefixed by any return type then it is assumed as int. In such cases if you do not return any int value by using keyword return, a warning occurs during compilation- “Function should return a value”. This situation can be avoided by using return keyword in main or use void before main.
Function Prototype
l  The prototype describes the function interface to the compiler by giving details such as the number and type of arguments and type of return values.
 Its syntax is
      return type function name (argument list);

l  Function prototype is also known as function declaration.
l  If a function is declared outside any function that is globally, then function can be called from any part of the program.
l  If a function is declared inside a particular function, then it is called local declaration. This means function can be only called from the function where it is declared.

Function Call by Value
l  A function call by passing values
l  The called function creates a new set of variables and copies the values of arguments into them.
l  This mechanism is good if the function does not need to alter the values of the original variables in the calling program.

#include<iostream.h>   //header file contains declaration of cout and cin
int area(int,int);             // function declaration/prototype
void main()
{
  int l, b, a;
  cout<<“Enter length and breadth”;
  cin>>l>>b;
  a=area( l , b);              //function area() is called by passing two int values
  cout<<“Area of rectangle is “<<a;
}
int area(int L, int B)      //function definition
{
 return(L*B);
}

l  Here function area() is called by passing two int values, that is mentioned by writing variable names separated by comma in parenthesis.
l  Values of l and b are copied into variables L and B respectively.
l  Function can only access its own variables, therefore function area() cannot access l and b. This is the reason we copy value of l and b in L and B.
l  Function area() returns a value produced by product of L and B. This value is then stored in variable ‘a’ of function main().

Function call by passing address
l  There may some scenarios where we would like to change the values of variables in the calling program.
l  Addresses can always be stored in special variables called pointers.

#include<iostream.h>
main()
{
 int a,b;
 void swap(int *, int *);
 cout<<“Enter two numbers: “;
 cin>>a>>b;
 swap(&a,&b);
 cout<<“a=“<<a<<” b=“<<b;
}
void swap(int *p, int *q)
{
 int t;
 t=*p;
 *p=*q;
*q=t;
}

l  Above program is of swapping the contents of two variables ‘a’ and ‘b’.
l  Notice the function call swap(&a, &b), here we pass addresses of variables ‘a’ and ‘b’ in place of values as we want to make changes possible in variable ‘a’ and ‘b’ from function swap().
l  These addresses get collected in pointers p and q.
l  swap () function returns nothing as it can directly makes changes in variables a and b using pointers p and q.

Function call by reference
l  A reference is implicit pointers that for all intents and purpose act like another name for a variable.
l  C++ permits us to pass parameters to the functions by reference.
l  The formal arguments in the called function become aliases (alternative name) to the actual arguments.
#include<iostream.h>
void main()
{
 int a,b;
 void swap(int &, int &);
 cout<<“Enter two numbers: “;
 cin>>a>>b;
 swap(a,b);
 cout<<“a=“<<a<<” b=“<<b;
}
 void swap(int &x, int &y)
{
 int t;
 t=x;
 x=y;
 y=t;
}

l  Here, function swap() is called by passing references of variable ‘a’ and ‘b’.
l  x and y are reference variables that is another names of ‘a’ and ‘b’ respectively
l  So we do not create extra copies of ‘a’ and ‘b’. 

Inline function
l  Function in a program is to save memory space which becomes appreciable when a function is likely to be called many times.
l  However every time a function is called, it takes lot of extra time in executing a series of instructions for tasks such as jumping to the functions, saving registers, pushing arguments into the stack and returning to the calling function.
l  So when function is small it is worthless to spend so much extra time in such tasks in cost of saving comparatively small space.
l  To eliminate the cost of calls to small functions, C++ proposes a new feature called inline function.
l  An inline function is a function that is expanded in line when it is invoked.
l  Compiler replaces the function call with the corresponding function code.
l  inline is a request not a command.
l  The benefit of speed of inline functions reduces as the function grows in size.
l  So the compiler may ignore the request in some situations.
Few of them:
·        Function containing loops, switch, goto.
·        Functions with recursion
·        Containing static variable.

#include<iostream.h>
inline int add(int, int);
void main()
{
  int a,b;
  cout<<”Enter two numbers”;
  cin>>a>>b;
  int sum=add(a, b);
  cout<<”Sum is “<<sum;
}
int add(int x, int y)
{  return(x+y); }


Default arguments

l  C++ allows us to call a function without specifying all its arguments. In such case function assigns a default value to the parameter which does not have a matching argument in the function call. Default values are specified when the function is declared.
#include<iostream.h>
int add(int,int,int z=0);
void main()
{
  int a,b,c;
  cout<<”Enter three numbers”;
  cin>>a>>b>>c;
  int s3=add(a,b,c);
  cout<<s3;
  int s2=add(a,b);
  cout<<s2;
}
int add(int x,int y,int z)
{
  return(x+y+z);
}

l  The default value is specified in a manner syntactically similar to a variable initialization. The above prototype declares a default value of 0 to the argument z.
l  Here, we have only one version of function add, which is capable of taking three arguments as well as two arguments.
l  When function add() has two parameters, then first value is received in x, second value is received in y and there is no value for z but z is by default containing 0.

Function overloading

l  Overloading refers to the use of same thing for different purposes. Function overloading means, we can use the same function name to create functions that perform a variety of different tasks. This is called function polymorphism or compile-time polymorphism in OOP. Another is run time polymorphism which we’ll discuss later
l  The correct function to be invoked is determined by checking the number and type of the arguments but not on the function type (return type).

#include<iostream.h>-
float area(int);
int area(int,int);
void main()
{
  int  r;
  cout<<”Enter radius of a circle”;
  cin>>r;
  float a=area(r);    
  cout<<”Area of circle is “<<a;
  int l,b,A;
  cout<<”Enter length and breadth of rectangle”;
  cin>>l>>b;
  A=area(l,b);
  cout<<”Area of rectangle is “<<A;
}
float area(int R)
{ return(3.14*R*R); }
int area(int L, int B)
{ return(L*B); }

No comments:

Post a Comment