Definition of Function in C – User Defined Functions

Definition of Function in C

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; and
6. a return statement.

All the six elements are grouped two parts, namely,
♦ function header (First three elements);and
♦ function body (Second three elements).

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 statement1;
    executable statement2;
    . . . . .
    . . . . .
    return statement;

The first line function_type function_name(paramreter 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 (also known as return type), the function name and the 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 (like float or double) that the function is expected to return to the program calling the function. If the return type is not explicitly specified, C will assume that it is an integer type. If the function is not returning anything, then we need to specify the return type as void. Remember, void is one of the fundamental data types in C. It is a good programming practice to code explicitly the return type, even when it is an integer. The value returned is the output produced by the function.

The function name is any valid C identifier and therefore must follow the same rules of formation as other variable names in C. The name should be appropriate to the task performed by the function. However, care must be exercised to avoid duplicating library routine names or operating system commands.

Formal Parameter List – Definition of Function

The parameter list declares the variables that will receive the data sent by the calling program. They serve as input data to the function to carry out the specified 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 the calling programs. This aspects will be covered later when we discuss more about functions. The parameters are also known as arguments.

The parameter list contains declaration of variables separated by commas and surrounded by parentheses.

Examples :
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 parenthesis. Note that the declaration of parameter variables cannot be combined. That is, int sum (int a,b) is illegal.

A function need not always receive values from the calling program. In such cases, functions have no formal parameters. To indicate that the parameter list is empty, we use the keyword void between the parentheses as in
void printline (void)
. . . .
This function neither receives any input values nor returns back any value. Many compilers accepts an empty set of parentheses, without specifying anything as in
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 declarations and statements necessary for performing the required task. The body enclosed in braces, contains three parts, in the order given below

  1. Local declarations that specify the variables needed by the function.
  2. Function statements that perform the task of the function.
  3. A return statement that returns the value evaluated by the function.

If a function does not return any value (like the printline function), we can omit the return statement. However, note that its return type should be specified as void. Again it is nice to have a return statement even for void functions.

Some examples of typical function definitions are :

(a)  float mul (float x, float y)
       float result;                /* local variable*/
       result = x * y;              /* computes the product */
       return (result);             /* returns the result */
(b)  void sum (int a, int b)
       printf ("sum = %s", a+b);     /* no local variables */
       return;                       /* optional */
(C)  void display (void)
        {                            /* no local variables */
           printf ("No type, no parameters");
                                     /* no return statement */

Note  1. When a function reaches its return statement, the control is tranferred back to the calling program. In the absence of a return statement, the closing brace acts as a void return.

2. A local variable is a variable that is defined inside a function and used without having any role in the communication between functions.

Read More Topics
Overloaded function in C++
Function Declaration in C
Importance of C

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply