Function
Functions are the building blocks of C. All programs definitely consist of one function - main() - Which will call or invoke standard functions of C such as printf() and scanf(), etc., as well as user-defined functions.
Advantages of Funtions
functions also provides programmers a convenient way of designing programs in which complex computations can be built into the functions. Once properly designed, a programmer does not have to bother about how the calculations are done in the function; it is sufficient for him to know what it does. The programmer has to simply ensure that the required parameters are passed to the function.
Parameters of a Function
Consider the following printf() statement:
printf ("%d" , value);
The printf() function expects two pieces of information:
1. The format in which data is to be printed.
2. The variable whose value is to be printed in the specifed format.
These are the parameters of the function printf().
Thus, parameter of a function is the data that the function must receive when called or invoked from another function.
Consider the following examples:
main() main()
{ {
disp_head(); sum(x,y);
} }
disp_head () sum(a,b)
{ {
printf("Student Report"); int value;
} value=a+b;
}
Program A Program B
The function disp_head() in program A does not except any data when invoked. The function sum(), in program B, however, excepts two values as is evident from the code of that function. Accordingly, the function sum() is invoked from main() with two values being passed to it- the values of x and y. Note that x and y are separated by a comma.
The parameters of user-defined functions are declared outside the {} of that function, and are declared as the data type that the function excepts them to be.
Invoking Functions
In C programs, functions that have parameters are invoked in one of two ways:
1. Call by value
2. Call by reference
Call by Value
Consider the following code for the problem statement mentioned:
/* Program */
main()
{
int num1, num2;
char ch;
printf ("Enter 2 numbers and an operator");
scanf ("%d%d%c", &num1, &num2, &ch);
calc (num1, num2, ch);
/* calc() invoked with 3 parameters */
}
calc ( val1, val2, ch)
int val1, val2;
char ch;
{
switch (ch)
{
case '+' : printf ("%d", val1 + val2);
break;
case '-' : printf ("%d", val1 - val2);
break;
case '*' : printf ("%d", val1 * val2);
break;
case '/' : printf ("%d", val1 / val2);
break;
default : printf ("Invalid Operator");
}
}
In this program, values entered for the variables num1, num2 and ch in the main() function are passed to the function calc(). These values get copied into the memory location of the parameters val1 , val2 and ch respectively of the function calc() when it is invoked.
Call by Reference
Call by reference means that the called function should be able to refer to the variable of the caller function directly, and not create its own copy of the values in different variables. This would be possible only if the address of the variable are passed as parameters to the function.
Consider the same program written using call by reference:
/* Program */
main()
{
int num1, num2;
char ch;
printf ("Enter 2 numbers and an operator");
scanf ("%d%d%c", &num1, &num2, &ch);
calc (num1, num2, ch); /* calc() invoked with 3 parameters */
}
calc ( val1, val2, ch)
int *val1, *val2;
char *ch;
{
switch (*ch)
{
case '+' : printf ("%d", *val1 + *val2);
break;
case '-' : printf ("%d", *val1 - *val2);
break;
case '*' : printf ("%d", *val1 * *val2);
break;
case '/' : printf ("%d", *val1 / *val2);
break;
default : printf ("Invalid Operator");
}
}
In this program, the addresses of the variables num1, num2 and ch are passed as parameters to the function calc(), instead of their values.
Note that the parameters val1, val2 and ch are accordingly declared as pointer type data. The variables val1, val2 and ch then directly point to the memory locations on num1, num2 and ch their values are used in the function calc(). Therefore, in the switch statement, *ch, *val1, and *val2 are used to refer to the contents of oper, val1 and val2.
Passing Arrays to Functions
Arrays are passed to functions by the call by reference method. For instance, if an array called num_array of size 10 is to be passed to a function called stringfunc(), then it would be passed as follows:
stringfunc(num_array);
Recall the num_array is actually the address of the first element of the array, i.e. num_array[0]. So, this would be a call by reference.
The parameter of the called function, say number_list could be declared in any of the following ways:
stringfunc (number_list)
int number_list[]; /* no subscript */
{
:
}
OR
stringfunc (number_list)
int number_list[10]; /* Same size as num_array */
{
:
}
OR
stringfunc (number_list)
int *number_list; /* int type pointer because */
{ /* number_list is an int type */
: /* array */
}
Returning Values from a Function
As data can be passed to a function, data can also be passed back from a called function to its caller.
In C, functions can return values through the return verb as illustrated in the following example:
main()
{
int x, y, value;
scanf ("%d %d %d", &x, &y);
value = sum (x, y); /* function sum() invoked */
printf ("Total is %d ", value);
}
sum (a, b)
int a, b;
{
return a + b ;
}
In this example, the function sum() sends back the value of a + b to the function main(). The value returned to main() from sum() is stored in a variable called value, the value of which is printed out through the printf statement in main().
The return statement
Note that a function can return only one value,
Returning an Array from a function
Since an array is always passed to a function through a call by reference, it is easy to alter the condition of the array directly in the function. So, arrays do not have to be returned from a function explicitly using the return statement.
Copyright © 2008 Manikkumar. All rights reserved.