Add to Technorati Favorites
Google

Functions - Basic Concepts

Today, we will see yet another concept present in any programming language. Naturally, we will see why at all we need functions. How a function is declared and defined in C++? What new terms are related with functions which are not there in C?

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 function_name(argument_declaration_list);

where,

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:

  1. Call-By-Value
  2. Call-By_Reference using Pointer Variable
  3. 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:
- Increased Performance: No overhead of copying large data
- Weaker Security: Caller's data can be corrupted
  • Call-By-Reference Using Reference Variables:
- Increased Performance for same reason as above
- Data is secure

Now, let us see how functions are declared, defined, and used in each of the 3 cases:

  • Call-By-Value

In Calling Program:
int a = 5;
int b = 6;
swap(a,b);

Function Declaration:
void swap(int a,int b); or simply
void swap(int,int);

Function Definition:
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
}


  • Call-By-Reference using Pointers
In Calling Program:
int a=5,b=6;
swap(&a,&b);

Function Declaration:
void swap(int *a,int *b); or simply,
void swap(int *,int *);

Function Definition:
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
}

  • Call-By-Reference using Reference Variables:
In Calling Program:
int a=5,b=6;
swap(a,b);

Function Declaration:
void swap(int &a, int &b);

Function Defintion:
void swap(int &a, int &b)
{
int temp;

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.

No comments: