Tuesday 13 November 2012

Functions in C++:

About main function:
In C++, default return type of main function is int.
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: 
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);



Function prototype is also known as function declaration.
If a function is declared outside any function that is globally, then function can be called from any part of the program.
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: 

A function call by passing values
The called function creates a new set of variables and copies the values of arguments into them.
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);
}



Here function area() is called by passing two int values, that is mentioned by writing variable names separated by comma in parenthesis.
Values of l and b are copied into variables L and B respectively.
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.
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 :
There may some scenarios where we would like to change the values of variables in the calling program.
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;
}



Above program is of swapping the contents of two variables ‘a’ and ‘b’.
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().
These addresses get collected in pointers p and q.
swap () function returns nothing as it can directly makes changes in variables a and b using pointers p and q.



Function call by reference: 

A reference is implicit pointers that for all intents and purpose act like another name for a variable.
C++ permits us to pass parameters to the functions by reference.
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;
}



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









No comments:

Post a Comment