Saturday, 22 June 2013

Functions II


In the previous post I had talked about main functions and function prototyping. In this post I would like to continue on functions and cover all the areas around them.

Call By Reference

Limitations of Call by Value

In C language a function call passes arguments by value. The Called function creates a new set of variables and copies the values of arguments into them. The function does not have access to the actual variables in the calling program and can only work on the copies of the actual values. This mechanism works fine until the function does not need to alter the values of the original variables in the calling program. 

But such demanding situations might arise where we need to change the values of the variables in a calling function. Such as in the case of Bubble sort problem where we need to compare two adjacent elements in the list and interchange their values according the the comparison results if needed. If a function is used for creating a Bubble sort algorithm then it should have the capability to alter the values of variables in the calling functions, which is not possible by call by value method.

Call By Reference

In C++ we use reference variables which enable us to pass parameters to the functions by reference. When we pass an argument by reference, the formal arguments in the called function becomes aliases to the actual arguments in the calling functions. This means that when the function is working with its own arguments it is actually working on the original data. 

An example would make things appear easier, 

void swap ( int &a , int &b )        //a and b are reference variables 
{
         int t = a;
         a=b;
         b=t;
}

Now if m and n are two integer variables, then the function call 

swap (m,n);

will exchange the values of m and n using the aliases a and b.

In C languages this is accomplished by using pointers and indirections.

Return By Reference

A function can also return a reference. Consider the following function :

int & max (int &a, int &b)
{
         if (a>b)
                 
            return a;
         else
           
            return b;
}

Since the return type of max() is int&, the function returns references to a or b. Then a function call such as max (x,y) would yield a reference to either x or y depending on their values. This means that this function call can appear on the left hand side of an assignment statement. That is, the statement

max (x,y) = -1;
is legal and assigns -1 to x if it is larger otherwise -1 is assigned to y.

In the next post I would stress on inline functions, default arguments and function overloading.









Thursday, 20 June 2013

Functions in C++

The Main Function


C language does not specify any return type for the main () function which is the starting point in the execution of the program. The main() definition looks something like this in C -

main()
{

       // main program statements and instructions

}

This is perfectly valid in C as main() is C doesnt return any value.

In contrast C++ has a return value of type int for the main () to the Operating System. The main function in C++ is, therfore defined as follows :-

int main ()
{

 //program statements

return 0;
}

Note :- Since the return type of functions is int by default, the keyword int in the main is optional.

Many Operating systems test the return value to determine if there is any problem. The normal convention is that an exit value of zero means that the program ran successfully, while a nonzero value means there was a problem. The explicit use of a return 0 statement will indicate that the program was successfully executed.

Function Prototyping

Function prototyping is one of the major improvements added to C++ functions from C functions. The prototype describes the function interface to the computer by giving details such as the number and type of arguments and the type of the return value. 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 are passed, and the return value is treated correctly. These checks and controls didnt exist in the conventional C functions.
 
Function prototype is a declaration statement in the calling program and is of the following form
 
type function-name (argument list );
 
The argument list contains the types and names of arguments that must be passed to the function.
 
Example
 
float volume (int x, float y, float z);
 
Note :- Each argument variable must be declared independantly inside the parentheses i.e. a combined declaration like
 
float volume (int x, float y, z) ; is illegal.
 
In a function declaration, the names of the arguments are dummy variables and therefore they are optional. That is , the form
 
float volume (int , float, float );
 
is acceptable at the place of the abovemetioned declaration. At this stage, the compiler only checks for the type of arguments when the function is called.
 
In the function definition though , names are required because arguments must have a reference inside a function.
 
Example :-
 
float volume ( int a, float b, float c)
{
 
       float v = a*b*c;
       .........................
       .........................
}
 
the function volume () can be invoked in a program as follows :
 
float cube1= volume (b1,w1,h1);   //Calling the function
 
The variables b1, w1, h1 are known as actual parameters which specify the dimensions of cube1. Their types should match with the types declared in the protoype.
 
We can also declare a function with an empty argument list, as in the following example :-
 
void display () ;
 
In C++, this means that the function does not pass any parameters. It is identical to the statement
 
void display ( void);
 
In C an empty parantheses implies any number of argument generally returning garbage values, in C++ though we have a default void type if the parantheses is left empty.
 
Hope to see you again for the next topic.
 
Note :- Each argument variable must be declared independantly inside the parantheses.