First, we will look at the role of function. Suppose, you want to delegate a task then you will not like to tell each time how to perform that task. Instead, you will tell it once to the person whom you are delegating the task and whenever, you want the task to be done then you will simply call the person to do the task again. The role of function is similar to this situation only. If you have some logic to be repeated then you will put this logic in a function and call this function wherever required. Obviously, this do away repitition and makes the code more maintainable.
Now, let us see how to declare a function. The syntax for function declaration is:
data_type returned can be any valid data type we have seen in previous posts, for e.g., int, double, char, int * etc.
If a function returns 'void' then it is more appropriately called a pure procedure.
argument_declaration_list is of the form:
data_type1 param1, data_type2 param2,..data_typeN paramN
where again data_type1..N can be any valid data type.
In C++, arguments can be passed to functions using any of the following 3 ways:
- Call-By_Reference using Pointer Variable
- Call-By-Reference using Reference Variable
In call-by-value, argument value is not altered in calling program but if passed using call-by-reference then changes done to the value of argument in the called function also gets reflected in the calling function.In natural sense, call-by-value is like giving a photocopy of a paper to your friend so that he can prepare a gist of it. Now, you are only interested in gist and not on what he does to that paper - he may write on it or do underlining. However, call-by-reference is like giving paper to your friend to do any corrections on the paper itself.
Now, question is when to use call-by-reference using pointer variable and when by reference variable. First, let us understand how these 3 techniques work.
- In call-by-value, the formal parameter of the function gets a copy of the actual agrument. As such, changes to passed variable is not reflected in the calling program.
- In calling using pointer, the address of the variable is passed to the called function and therefore, any changes done are reflected in calling program as well.
- Reference variable is nothing but an alias for a variable. In terminology of compilers, the symbol table will have two entries for each name and addresses of these variables will be same. Again, therefore, changes to variable in called program are visible in the calling program.
Now, you must be wondering how to decide when to use call-by-reference using pointer and when to use call-by-reference using reference variable. Here are rules that help in making this decision:
- Call-By-Value: Used for small & non-modifiable arguments
- Call-By-Reference Using Pointers:
- Weaker Security: Caller's data can be corrupted
- Call-By-Reference Using Reference Variables:
- Data is secure
Now, let us see how functions are declared, defined, and used in each of the 3 cases:
In Calling Program:
int a = 5;
int b = 6;
void swap(int a,int b); or simply
void swap(int a, int b)
temp = a;
a = b;
b = temp;
- Call-By-Reference using Pointers
void swap(int *a,int *b); or simply,
void swap(int *,int *);
void swap(int *a, int *b)
temp = *a;
*a = *b;
*b = temp;
- Call-By-Reference using Reference Variables:
void swap(int &a, int &b);
void swap(int &a, int &b)
temp = a;
a = b;
b = temp;
Points to Ponder
- Each of the 3 cases have different function declaration. In call-by-value, for argument list, we have data-type followed by variable name. In call-by-reference using pointers, we have dereferencing operator (*) in-between data-type and variable name. In call-by-reference using reference variable, we have address-of (&) operator in-between data-type and variable name.
- Just by looking in the calling program, you cannot tell whether call-by-value is used or call-by-reference using reference variable. However, if call-by-reference using pointers is used then you will have (&) in-between data-type and variable name in the calling program.
- Call-by-reference using reference variable has neatier code than inside function using call-by-reference using pointers .
- Most importantly, in the first case i.e. in call-by-value, the variables in the calling program will not be swaped but in the last two cases they will do for the reasons we have learned earlier in this post.
C++ adds many more concepts to functions like default arguments, inline functions, overloaded functions, over-ridden function, virtual function, etc.
We will see these concepts in next post.