MY mENU


Wednesday 15 February 2012

PassByValue and PassBy Reference in C functions


 Arguments can be passed to a function by two methods, They are

1.pass by value
2.pass by reference


Pass by Value:
                 Function in C passes all arguments by value. When a single value is passed to a function via an actual argument, the value of the actual argument is copied into the function.
 Therefore, the value of the corresponding formal argument can be altered within the function, but the value of the actual argument within the calling routine will not change. This procedure for passing the value of an argument to a function is known as passing by value.

Example:

Pass by Value



Pass by Reference:

                When passing by reference technique is used, the address of the data item is passed to the called function. Using & operator we can determine the address of the data item. Note that function once receives a data item by reference, it acts on data item and the changes made to the data item also reflects on the calling function. Here you don't need to return anything to calling function.

Example:

Pass by Reference


Recursion:

 Recursive functions are those functions, which call itself within that function. A recursive function must have the following type of statements.

1.A statement to test and determine whether the function is calling itself again.

2.A statement that calls the function itself and must be argument.

3.A conditional statement (if-else)

4.A return statement.

Example: Factorial of a number

             This is the most famous program on recursion. Many versions of this program are available.
All programs differ only in checking conditions. I prefer to write like the following one.

Recursion




No comments:

Post a Comment