by Dinesh Thakur Category: Function and Pointer

A function definition comprises a head (or header) and a body. The header gives the information about type of function, name of function, and a list of parameters. The list of parameters comprises types and names of parameters enclosed in parentheses and separated by commas. An illustration of function definition is given below.

type identifier (type parameter 1, type parameter 2, ---,type parameter n)

{ statements ;}

Definition starts with the type of function. It is the type of data that the function returns. A function returns only one value. If a function returns an integer value (type int) the function type is int; if a function returns a character or a string of characters its type is char. Similarly, if a function returns a value which is a floating point number, the type of function may be float or double. However, there are functions which do not return any data, they simply carry a message or perform an operation. The type of such functions is void. The second word in the definition is the name or identifier of the function. The function name may be chosen as we do for any variable. The identifier or name is followed by a list of parameters that the function takes for its operation. The parameter list is enclosed between brackets. The type of each parameter has to be given individually. The function body which is enclosed between curly braces ({ }) may consist of several lines of code comprising declaration of additional variables required for the function, statements, expressions, and a return statement. For void functions, the return statement is optional but for others it is a must. It should be noted that function returns only one value. A function does not return a value of type function or array but can return a pointer to an array or pointer to a function. Similarly, it does not return a value with const or volatile qualification but can return pointer to such a value.

For the sake of illustration, suppose we want to define a function to calculate and return the value of area of a rectangle if its length and width are given. The function definition may be written as below.

int Area_rect (int Length, int Width) //no semicolon

{ return Length*Width ;}

In the above code the first line is the function head and the second line is the function body. Here, int is the type of function (it returns int type data), Area_rect is the name of function. The parameters of the function are Length and Width, both are of type int. There is only one statement in the body of the function, i.e., return Length * Width;. Note that function head is not followed by a semicolon, while the statements in the body are followed by semicolons. Similarly, we may define more such functions. A function to calculate area of a circle may be defined as below. In this, the radius is taken of type float and area of type double.

double Area_circle (floatRadius)

{ return 3.14159*Radius*Radius;}

A function to simply display a message may be defined as below. Such functions are also called functions with empty parameter list.

void Display (void)

{printf ("C is a popular computer language."; }

Return Statement

In the definitions of functions Area_rect and Area_circle given above you must have noticed that the last statement is return value. When a function is called, the program control passes on to the called function. The return statement passes on the resultant value to the calling function and with this the program control also goes back to the calling function. The return statement for a void function is optional, because, they do not return any value and the program control automatically goes to calling function on occurrence of the last closing brace(}) of the called function. If it is desired to put the return statement the code is given below, however, it is optional.


For return type functions, the last statement is the return statement as illustrated below.

return value;

In the definition of function Area rect the return statement is written as:

return Length* Width;

An example of function definition and function call is illustrated.


int Area_Rect (int Length, int Width) // function definition
{return Length *Width;}
int main ()
   int a=40, b=20, c=15, d=10;
   int A, B, C;
   A= Area_Rect (a, b); // function call, Length= a, Width =b
   B =Area_Rect (c, d); // function call, Length= c, Width= d
   C =Area_Rect (40, 20); // function call Length= 40, width= 20
   printf ("A=%d\t B=%d\t C=%d\n", A, B, C);
   return 0;

Function Definition in C

Where to Place Function Definition in a Program

We have already discussed that a function cannot be defined in another function. Therefore, a function definition has to be outside the main function, i.e., at the file scope. Therefore, a function may be defined above the main function or below the main function as illustrated in Fig. If the function definition is given below main function, then the function head or its prototype must be declared above main.

       Function Definition in C

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles