Types of user-defined functions in C programming

Aside from using predefined function provided by C programming. We can create our own function to perform specific task in program. Those function created by user are called user-defined functions.

Each C program must have at least one function, which is main().

Example of user-defined function


//Program that uses a function to calculate and print the area of a rectangle

#include <stdio.h>
int calculateArea(int a, int b);  //function prototype
int main()
{
  int length, breadth, area;
 
   printf("\nEnter the Length of Rectangle : ");
   scanf("%d", &length);
 
   printf("\nEnter the Breadth of Rectangle : ");
   scanf("%d", &breadth);
 
   area = calculateArea( length * breadth ); //function call
   printf("\nArea of Rectangle : %d", area);
 
   return (0);
}                     //end main

int calculateArea(int a, int b)    //function definition
{
  int area;
  area = a*b;
  return area;  //returns calculated area
}      

Output

Enter the Length of Rectangle  : 7
Enter the Breadth of Rectangle : 2
Area of Rectangle : 14

Function Prototype


Function prototype is just the declaration of the functions that are or may be used in the program. It doesn’t contains body/code. 

Syntax:

returnType functionName(type1 arg1, type2 arg2,...);

In the above example, int calculateArea(int a, int b);  is the function prototype. Function prototype must contain:

  1. function name  calculateArea
  2. return type int
  3. arguments it takes int a, int b

Defining the function prototype is not mandatory.

Calling a function


To understand how function call works look into functions in C programming

Syntax

returnType functionName(type1 arg1, type2 arg2, ...);

In runtime if the compiler encounter the function, the control of the program execution is transferred to function definition. 

A function can call itself and it is known as “Recursion“.

Example of Recursion

void recursion() {
   recursion(); /* function calls itself */
}

int main() {
   recursion();
}

Here, funtion recursion() calls itself. For the complete tutorial on Recursion in C programming.

Function Definition


Function definition is where logic of function is.  A function may need arguments. 

Syntax for function that takes argument

returnType functionName(type1 arg1, type2 arg2, ...);
{
    //body of the function
}

Syntax for function without argument

returnType functionName();
{
    //body of the function
}

If function returns then its function name must be preceded by its return type. If function doesn’t return then its return type must be void

Return statement


We can take return statement as the end of the function call.  In the example above the statement return area; returns the calculated area inside the function to the function call. 

Syntax of return statement

return (expression);

In above example, return area; return the calculated value. But we can change it to 

return a * b;

As area in above example corresponds to a *b . Hence,

//as area = a* b ;
return a * b is equal to return area;