| Functions |
| |
| Function Definition & Call |
| |
| Once the compiler knows that you have declared a function, it then needs the definition of that function. |
| |
| Defining a function means writing the statements that you want to group under that function. Hence the function definition contains the code (or statements of the function). |
| |
| The first line of the definition is the Declarator. Don’t confuse declaration with declarator. The declarator is very similar to the function declaration except it does not have the statement terminator. |
| |
| The declarator must agree with the declaration. It must have the same function name, and the same types of arguments in the same order and the same return type. |
| |
| The general syntax of the three components of a function are as follows: |
| |
| // Function declaration |
| |
| Return data_type function_name (data type of parameter1, data type of parameter2)
int main ( ) |
| |
| { |
| function _ name (arguments); // This is function call. It comes within the main function |
| } |
| |
| // Funtion definition |
| |
| Return data_type function_name(arguments) // Function Declarator |
| |
| { |
| Body of function; // Function Code |
| } |
| |
| Perhaps you feel a bit vague about the concepts of function. Don’t worry. It should become clear with an example. |
| |
| Example: function to add two numbers. |
| |
 |
| |
| Out put of the program |
| |
 |
| |
| The above program is quite a simple example to illustrate the three components of a function. |
| |
| As you can see, we have first declared that a function of the return data type void, with name add and no arguments.
Then comes, as usual, the main function. |
| |
| Remember: The compiler will always execute what you type in the main function. You can execute other functions by calling them within the main function. The compiler will run the main function. |
| |
| Since We just want to illustrate functions with a simple example, We haven’t written any code in the main function other than the function call. |
| |
| When the compiler reads the function call statement, it knows that you had already declared a function by the same name and argument type. |
| |
| So it goes on reading your program and comes to the function definition. In this you have written a set of statements to find the sum of two numbers. |
| |
| The compiler will execute these statements. After displaying the result (due to the cout keyword in the add function), the compiler will go back to the main function. It will then see whether you have written anything after the function call. In this program We haven’t mentioned anything, except the end of the program. |
| |
| If you remember, We said that the function declaration will tell the compiler that a function by that name is coming up later on in the program. But there is a way to avoid writing the function declaration, i.e. there is no need to declare the function. |
| |
| If you do not declare the function then, the function definition must come before (precede) the function call. |
| |
| In example 1, the function call came before the function definition and hence we had to declare the function at the beginning. |
| |
| Let us consider the same program to add two numbers using a function called add with no arguments. |
| |
 |
| |
| Out put of the program |
| |
 |
| |
| As you can see, in the above program there is only one small change. We haven’t declared the function because We defined it before calling it. When the compiler reads the above program, it will first read the function definition. |
| |
| Argument |
| |
| Argument is a piece of data passed from program to function. Argument allows a function to operate with different values. |
| |
| The variables used within the function to hold argument values are called parameters. The data type in the function declaration and function call should be the same. Maybe it all sounds a bit hard to understand. Just read on. |
| |
| Consider the following program to explain arguments. Again consider the program to add two given numbers but using function arguments. |
| |
| Example: |
| |
 |
| |
| As you can see in the above program, We have used two parameters within the function ‘add’. The function definition comes before the function call and hence We have not declared the function. Up till this point, everything is the same. |
| |
| Now observe the Decelerator statement. The parameter specified is int var1, int var2 |
| |
| What this means is that in the function named add, var1 and var2 are integers that are used in the body of the function. In the body We have declared a third integer, namely int var3. |
| |
| var1 and var2 are added and stored in var3 using the statement: |
| |
| var3=var1 + var2; |
| |
| The last statement of the function add, is just to display the value of var3. |
| |
| Now the question might arise, what are the values of var1 and var2? A good question. To get an answer to that just read on. |
| |
| Next the compiler comes to the main function. So far the compiler has only read through the code. When it comes to the main function it will start performing. Two integers have been declared and defined as int num1 and int num2. |
| |
| The values of num1 and num2 are obtained from the user. After obtaining the values, you see the statement: |
| |
| add(num1,num2); |
| |
| As you know, add is the name of a function that has already been defined. So this statement is the function call. In the brackets, We have mentioned the two arguments namely num1 and num2. |
| |
| These two values have already been obtained from the user. The compiler then goes to the function definition of add. Over there we have mentioned the arguments as int var1 and int var2. |
| |
| But remember that the data type of the argument is the same. Num1, num2 are integers and so also are var1 and var2. |
| |
| What the compiler does next is to assign the value of num1 to var1 and num2 to var2. Thus now, |
| |
| var1=num1 and var2=num2. |
| |
| The body of the function add is now executed. |
| |
| Hence, in the above program the arguments are passed from the program (which is the main function) to the function add. |
| |
| The variables used to hold the argument values are known as parameters. The function declarator in the function definition specifies both data type and name of parameters. |
| |
| In our example, the parameters were var1 and var2. Their data types was int (integer). 'num1' and 'num2' are the arguments passed to the function. |
| |
| If we had used the function declaration then it would have been as follows: |
| |
| void add (int, int); |
| |
| In the function declaration you don’t have to specify the parameters. |
| |
| |
|
| |
| |