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, 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 void. In C, one function cannot be embedded within another function.
Function Prototype
The function prototype declares a function before 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 before the function is called. A function defined before main() (forward declaration) does not need a function prototype. The variables passed to a function are called arguments, and 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.
Function Call
A program cannot execute the function statements until 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 must 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 control passes to the place from which the function was called.
Function Parameters
The purpose of parameters is to allow functions to receive arguments. A comma separates multiple parameters. 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 x and y will only be changed by passing the values by reference.
#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; }
Passing an Array of Values to a Function
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 in the function will therefore be reflected in the original array. To pass a single-dimension array as an argument in a function use 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 will be received.
The following simple function 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; }
Functions That Have a Variable Number of Arguments
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 first followed 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 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() - 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”.
#include <stdarg.h> #include <stdio.h> // this function sums 5 numbers int sumvalues(int arg_value, ...) { int i,sum; va_list ap; // va_list holds information about variable arguments va_start(ap, arg_value); // va_start must be called before accessing variable argument list sum=va_arg(ap, int); // Initialise sum as first argument in list //iterate through arguments and add to total for (i = 2; i <= arg_value; i++) { sum=sum+va_arg(ap, int); } va_end(ap); // va_end executed before the function end return sum; } int main() { int count = 5; printf("sum of value is %d", sumvalues(count, 12, 67, 6, 7, 100)); return 0; }
Returning a Value from a Function
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.
Recursion
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.
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 }