Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




Function in c++

Function is a part of program that performs a particular task whenever it is called. It is also called ‘sub-program’. It can be called from several different places in the program. The function can take no or one or more arguments from the calling function and it may or may not return a value to the calling function with the help of return statement. The arguments given in the function call are called ‘Actual Arguments’ and those given in function definition are called ‘Formal Arguments’.

A function can be called by value, by address or by reference. These three methods are described below: Click on links for details.

1. Call by value : In this method the values of the actual arguments are copied to the formal arguments. Since the formal arguments are only the copies of the actual arguments, if we do any changes in the formal arguments they do not affect the actual arguments.                    
                                                                                               Click for detail and example


2. Call by address : In this method the addresses of the actual arguments are copied to the formal arguments. Since the formal arguments point to the actual arguments as they contain addresses of the actual arguments, if we do any changes in the formal arguments they will affect the actual arguments. The formal arguments will be pointers because we have to store addresses in them.                                                                                                        Click for detail and example


2. Call by reference : In this method the reference variables of the actual arguments are created as formal arguments. Reference variables are aliases of the original variables and refer to the same memory locations as the original variables do. Since the formal arguments are aliases of the actual arguments, if we do any changes in the formal arguments they will affect the actual arguments.                                                                                                        Click for detail and example



Share this article   |    Print    |    Article read by 3236 times
Author:
Rohit kakria
I am software developer, moderator of xpode.com
Related Articles:
Related Interview Questions: No related interview question