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

## C++ Functions

 Module Index ► Function Declaration and Definition ► Parameter Passing ► Default values for arguments ► Function Overloading

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.

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 :

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 :

Default values for arguments
We can define default values for end arguments in the function definition. If the corresponding values are not passed during function call, default values for those arguments are used. Following program illustrates this :