A function is a unit of code grouped to perform a specific task. Functions provide a way to compartmentalise and organise a program. Functions are identified by a name and enclosed within scope brackets and may be terminated by a return statement. On calling a function the execution of the program branches to the first statement in that function and continues until it reaches a return statement or the last statement of the function; at that point, execution resumes at the place where the function was called. A function can have multiple parameters separated by commas, but it can have only one return type. A function that does not need to return a value is declared as void. In C, one function cannot be embedded within another function.
The function prototype declares a function prior to its definition and details its name, the list of parameters the function accepts and the return type of the function. The compiler needs to know this information prior to the function being called. A function defined before main() (forward declaration) does not need a function prototype. The variables passed to a function are called arguments, and they are enclosed in parentheses following the function’s name. A function declaration inside main(), has its scope limited to within the main{} block, however, if you add it outside main, its scope is the entire source file.
A program cannot execute the statements in a function until the function is called by another part of the program. A function call is an expression that passes control and arguments (if any) to a function. When a function declaration contains parameters, the function call needs to send arguments. Arguments are values the function requests within its parameter list. Except for those functions with variable-length argument lists, the number of arguments in a function call must be the same as the number of parameters in the function definition. When the function is finished, execution passes back to the place from which the function was called.
The purpose of parameters is to allow functions to receive arguments. Multiple parameters are separated by a comma. These parameters don’t have to be of the same data type. Any valid C expression can be a function parameter, including constants, logical expressions, and other functions that return a value. Parameter data can be passed into and out of methods and functions using pass-by-value or pass-by-reference.
Pass by Value-Using "pass by value" means that the data associated with the actual parameter is copied into a separate storage location assigned to the function parameter. The parameters passed to a function become local variables within that function, even if they have the same name as variables within the scope of the statement calling the function. Any modifications to the function parameter variable inside the called function or method affect only the local parameter. This is the default method for passing an argument to a function.
Pass by Reference-Using "pass by reference", means that the function parameter will pass a reference (or pointer) to the variable in the calling environment. Any changes to the function parameter are reflected in the calling environment. actual parameter.
The following code section demonstrates call-by-reference and call-by-value by passing two values to two functions and then swapping them. The relative value of both x and y will only be changed by the call-to-reference function
#include <stdio.h>
void swapbyref(int *x, int *y); /* function declaration */
void swapbyval(int x, int y); /* function declaration */
int main () {
int a = 10,b=20;
printf("Before swap by ref of a and b : %d %d\n", a,b );
swapbyref(&a, &b);/*call swap function by ref*/
printf("Before swap by ref of a and b : %d %d\n", a,b );
printf("Before swap by value of a and b : %d %d\n", a,b );
swapbyval(a, b);/*call swap function by value*/
printf("Before swap by value of a and b : %d %d\n", a,b );
return 0;
}
/*swap function by ref*/
void swapbyref(int *x, int *y) {
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}
/*swap function by value*/
void swapbyval(int x, int y) {
int temp;
temp = x;
x = y;
y = temp;
return;
}
When an array is passed to a function it is always treated as a pointer by that function. Pointers to an array are passed as an argument by specifying the array's name without an index. The argument represents the memory address of the first element of an array. Any changes made in the function will therefore be reflected in the original array. To pass a single-dimension array as an argument in a function using one of the following.
void myFunction(int *param) array passed as pointer
void myFunction(int param[10]) array passed a sized array
void myFunction(int param[]) array passed as unsized array
All these variants are functionally identical. Each effectively tells the compiler that an integer pointer is going to be received.
The following piece of code demonstrates how to pass a char array and int value to a procedure.
#include <stdio.h>
float total(float value[],int size);/*function prototype declaration containing parameter list*/
int main()
{
float avg, measurement[] = {223.1, 555, 22.63, 3.12, 31.5, 180};
printf("Total Values = %.2f", total(measurement,6));/*function call in printf statement*/
return 0;
}
float total(float value[],int size)/*start of function with 2 parameters*/
{
int i;
float total= 0.0;
for (i = 0; i < size; ++i) {
total += value[i];
}
return total;
}
Variable length arguments is a feature that allows a function to receive any number of arguments.`
When a function is declared with a variable argument list, the fixed parameter must be declared followed first by ellipses at the end of the parameter list to indicate additional arguments as below
Macros and a pointer found within stdarg.h are used to retrieve the arguments in the variable list. These arguments are-
va_list - A pointer data type which is used to access the individual arguments
va_start() - A macro used to initialise the pointer arg_ptr to point at the first argument in the variable list.
va_arg() - Is a macro used to retrieve each argument, in sequence, from the variable list.
va_end() - When all the arguments in the variable list have been retrieved this macro is used to “clean up”.
The following shortcode segment demonstrates variable-length arguments by adding up 5 values to a function sumvalues -
#include <stdio.h>
float total(float value[],int size);/*function prototype declaration containing parameter list*/
int main()
{
float avg, measurement[] = {223.1, 555, 22.63, 3.12, 31.5, 180};
printf("Total Values = %.2f", total(measurement,6));/*function call in printf statement*/
return 0;
}
float total(float value[],int size)/*start of function with 2 parameters*/
{
int i;
float total= 0.0;
for (i = 0; i < size; ++i) {
total += value[i];
}
return total;
}
Functions can return a value or return nothing (void). To return a value from a function, the keyword return is followed by the value to return. A function’s return value is copied into a placeholder on the stack. The return value is popped off the stack and can be assigned as the value of the function. If the value of the function isn’t assigned to anything, no assignment takes place, and the value is lost. A function can contain multiple return statements but only one return value. Multiple return statements can be used to return different values from a function. After a return statement, program execution continues immediately after the statement that called the function.
A function that calls itself is known as a recursive function. The recursion proceeds until some condition is met which breaks the recursive cycle. The following simple recursion function demonstrates how a countdown procedure calls itself until the exit condition is met.
This simple piece of code to demonstrates recursion by a function calling itself until the value of variable countp is smaller than one
void printFun(int countp)
{
if (countp < 1) //exit function if value of countp is smaller than 1
return;
else
{
printf("Count value = %.i \n", countp);
printFun(countp-1); //recursive function calls itself
return;
}
}
int main()
{
int count = 3;
printFun(count); //call recursive function
}
Last Updates:25 October 2022