Definition of Functions


A function definition, also known as function implementation shall include the following elements :

  1. function name;
  2. function type;
  3. list of parameters;
  4. local variable declarations;
  5. function statements;
  6. a return statement;

All the six elements are grouped into two parts, namely,

A general format of a function definition to implement these two parts is given below :

function_type function_name(parameter list)
{
  local variable declaration;
  executable statement;
  return statement;
}

The first line function_type function_name(parameter list) is known as the function header and the statements within the opening and closing braces constitute the function body, which is a compound statement.


Function Header

The function header consists of three parts: the function type, the function name, and the formal parameter list. Note that a semicolon is not used at the end of the function header.


Name and Type

The function type specifies the type of value that calls the function and expects to return to the program. If the type of return is not specified. C will assume that it is an integer type. If the function is not returning anything, we need to specify the return type as void. Remember, void is one of the basic data types in C. It is a good programming practice to clearly type the return type even after integers. The value returned is the output produced by the function.

The name of the function must be a valid C identifier and therefore follow the same rules of structure as other variable names of C The name should be appropriate for the task performed by the function. However, care should be taken to avoid duplicating the library's routine name or operating system command.


Formal Parameter List

The parameter list declares the variables that will receive the data sent through the calling program. They serve as input data to the function to perform the specific task. Since they represent actual input values they are often referred to as formal parameters. These parameters can also be used to send values to calling programs. This aspect will be covered when we discuss more functions. Parameters are also known as arguments.

float quadratic (int a, int b, int c){....}
double power(double x, int n){....}
float mul(float x, float y){....}
int sum(int a, int b){....}

Remember, there is no semicolon after the closing bond. Note that the declaration of parameter variables cannot be combined. That is, int sum (int a, b) is invalid.

Values should not always be retrieved from an activity-calling program. In such cases, the functions have no formal parameters. To denote that the list of parameters is empty, we use the word zero in the first brackets.

void printline(void)
{
  .....
}

This function does not accept any input values or return any values. Many compilers accept an empty set of parentheses without specifying something as such

void printline()

But, it is a good programming style to use void to indicate a nill parameter list.

Function Body

The function body contains the necessary declarations and statements to perform the required functions. The body is bound by braces, in the following order:

  1. Local declaration that specifies the variables required by the function.
  2. Function statement by performing the function.
  3. A return statement that returns the value assessed by the function.

If a function does not return a value, we can omit the return statement. However, this is not to say that the type of return should be specified as invalid. Again, it is better to get even a return statement for invalid functions

float sum(int a, int b)
{
  float result;
  result =  a*b;
  return result;
}