The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. ― Donald E. Knuth

# C Tutorial

## Functions in C

 Module Index ► Function Declaration and Definition ► Parameter Passing ► Variable Length Argument List

A function is a block of statements which perform a specific task. A program can be divided into functions. This enhances the modularity and maintainability of a program. Functions eliminates redundant code i.e it helps us to avoid rewriting the same logic again and again. Functions are the means to support code reusabilty in C.

Function Declaration and Definition
Every C program has at least one function i.e main( ) where the program execution begins. Consider the following C program in which we use a function to add two numbers :

Function Declaration / Prototype describes the function interface to the compiler giving information about the function name, return types and parameters (arguments). In the above program,
Return Type : int
Parameters / Arguments : int a, int b

Function Definition provides the code which constitutes the body of the function. A general form of C function definition is shown below :

 return_type function_name ( argument list ) {             function body }

Function Call invokes the function body to perform a specific task.

Please note that argument list may consists of 0 or more no. of arguments and the return type of the function may be void also in case the function doesn't return any value. Following program uses two functions to generate first 'n' terms of fibonacci series. In fibonacci series, ith term of the series is equal to the sum of (i - 1)th and
(i - 2)th term with first two terms as 0 and 1.
Fibonacci series : 0 1 1 2 3 5 8 13 21 ....

Another sample program which finds the maximum of three numbers using a function max (int a, int b) which finds the maximum of two numbers.

Parameter Passing
We have seen above that while calling a function we pass parameters/arguments.
There are two ways of passing parameters :
Call by Value : This method copies the argument value into the formal parameters of the function. Apparently, all the programs we have seen till now uses call by value. In this method, changes made to the argument values in the called function is not reflected in the calling function. Consider the program which finds the maximum value out of three numbers. In the function call maxThreeNum(x, y, z) , values in x, y and z are copied into variables a, b and c respectively.
Call by Reference :
This method copies the address of the arguments into the formal parameters of the function. In this method, changes made to the argument values in the called function is reflected in the calling function.
Following program illustrates the difference between call by value and call by reference :

A typical program in which call by reference is used is swapping of two numbers :

Variable Length Argument List
Consider a scenario in which a function is expected to accept any number of arguments i.e the no. of arguments is not known at compile time. To handle this kind of situation C offers an advanced concept of variable length arguments. We need to include stdarg.h header file to use this feature. Following program computes the average of the values passed to a function. The number of values to be passed is not known previously. So, we use a function which accepts variable length arguments.