User-Defined functions in C

user-defined functions in C

What are User Defined Functions in C Programming Language ?

In C programming, functions play a vital role, while programming for a problem. There are two types of functions in C programming, these are

  • Pre-Defined Functions
  • User-Defined Functions

C Programming, gives the flexibility of creating user-defined functions according to one’s need. Bur before we start what are user-defined functions, you need to know, that what functions are, you can study about them on our Functions in C page, by clicking on the button below.

How to Declare a User-Defined function in C

Declaration or prototype of a user-defined functions, enables the compiler to understood, a user-defined function is going to be used in this program. The prototype of a user defined function contains, the name of the function, and the list of its parameters/arguments, it doesn’t contain the definition of the function. The declaration of a function is done in the starting of the code, just after including the header files and before the starting of the main() function.

  • returntype functionName(arg1, arg2….);

The declaration of a function is followed by a semi-colon, indicating that it is just the declaration of the function, and the body of the function will be defined later in the program, however there is a small exception, i.e;

Note-: It is not necessary to give the declaration of a function, if the body of the function is defined before the main() function in the program.

#include <stdio.h>  
int area(int a, int b);  // protoype of the function
int main()
{
   int a,b,are;
   printf(" Enter the length of the rectangle - ");   \\ accepting length of the recatangle
   scanf(" %d",&a);
   printf(" Enter the breadth of the rectangle - ");  \\ accepting breadth of the recatangle
   scanf(" %d",&b);
   are = area(a,b);   \\calling user-defined function
   printf(" The area of the rectangle is : %d", are);  \\ printing the answer, that we get from area()
     return 0;
    
}

int area(int a, int b)  \\ defining the body of user-defined function area()
{
    int ar=0;
    ar=a*b;  \\ calculating the area of the rectangle
     return  ar;  \\ returning the value to the main function
}
Output
Enter the length of the rectangle - 5
Enter the breadth of the rectangle - 2
The area of the rectangle is : 10

How does a user-defined function works ?

In the above example, we have defined a user-defined function for calculating the the area of the rectangle. We have declared the function prototype in the starting of the program, just after including the header files.

  • int area(int a, int b)

Analyzing the prototype, we can determine that int is the return type of the function. area is the name of the function and int a and int b are the arguments or parameters that are used in this user-defined function for calculating the area of the rectangle. The line in the above example are = area(a,b) is the call to the user-defined function area(a,b), where the data is transferred from the main() function to area(a,b) for calculation, and after the calculation it is returned by the user-defined function, using the return keyword.

As we have discussed above, that if the user-defined function is defined before the main() function, than there is no need of a function prototype. Let’s see the above example only in a new way

#include <stdio.h>
int area(int a, int b)
{
    int ar=0;
    ar=a*b;
    return ar;
}

int main()
{
   int a,b,are;
   printf("Enter the length of the rectangle - ");
   scanf("%d",&a);
   printf("Enter the breadth of the rectangle - ");
   scanf("%d",&b);
   are = area(a,b);
   printf("The area of the rectangle is : %d", are);
    return 0;

}
Output 
Enter the length of the rectangle - 5 
Enter the breadth of the rectangle - 2 
The area of the rectangle is : 10