Tutorial 7.3: Defining a Function

Defining a Function
Function definition consists of two parts, the function header and the function body (block). The function header specifies the function's return value type, function name and function parameters. The function body defines the operation that the function performs when it is called. The general form of a function definition is as follows.

return-type function-name (parameters)
{
declarations and statements;
}

The function-name can be any valid identifier that is not a keyword such as int, break, char etc. Of course you can't use a name that already been assigned to another function. The parameters is the list of variable names and their types that carry the values received by the function from the caller. The values supplied to the function is called arguments. The return-type specifies the type of the value returned by the function. The type of value can be specified as any of the available type in C. If the return-type is omitted, int is assumed. The void return-type indicates that the function does not returns a value.

The following program uses a function GetAverage to compute an average of three values.
#include <stdio.h>

/% function prototype of calcAverage */
float calcAverage(float num1, float num2, float num3, int N);

int main()
{
 int N = 3;
 float n1, n2, n3;
 float average;
 num1 = 15.5;
 num2 = 20.2;
 num3 = 25.6;
 average = calcAverage(n1, n2, n3, N);
 printf("The average is %f\n", average); 
 return 0;
}

/* function definition of calcAverage */
float calcAverage(float num1, float num2, float num3, int N)
{
 float average;
 average = (num1 + num2 + num3) / N;
 return average;
}
Sample output:

The average is 20.433334

Explanation:

In the pre-processor section, we have the following line.
/% function prototype of calcAverage */
float calcAverage(float num1, float num2, float num3, int N);
This is a function prototype. It is there to tell the compiler the specification of a function. The function prototype includes specification for the return type, the parameters and the function name. Basically function prototype is the same as the function header plus the semicolon at the end.
/* function definition of calcAverage */
float calcAverage(float num1, float num2, float num3, int N)
{
 float average;
 average = (num1 + num2 + num3) / N;
 return average;
}
Lets look at the function definition of GetAverage. The function has four parameters namely num1, num2 and num3 which are of type float and an integer called N. It is defined to computes the average of three values and returns a float type value which is the average. The return statement is used when the function is required to return a value. If the function is defined not to return a value, we are not required to put the return statement because the control is returned simply when the close brace is reached. Note that the return value should be of the same type as the declared return type in the function header.

As usual, in function main(), we have variables declaration and assignment. In this case we have three float variables and a single integer variable. Then we have the function call statement.
 average = calcAverage(num1, num2, num3, N);
We simply call by writing the function's name followed by the arguments to be supplied to the function. It is important to note that the supplied arguments must be of the same type as the expected type. When the arguments are supplied to a function, copies of the values in n1, n2, n3 and N are made for the function. The copies are then stored in num1, num2 and num3 which have been declared in the function header. This means the function cannot modifies the values in n1, n2 and n3. This mechanism is called call-by-value. There is another mechanism called call-by-reference. These two mechanism will be discussed in the next section. The result (returned value) of the function is assigned to variable res. Finally the value of res is displayed using printf.

Next: Arrays

No comments:

Post a Comment