User Defined Functions in C

We can write user defined functions in C. In addition to standard library functions supplied by the system, C allows programmers to write their own functions for a specific work to accomplish. A function defined by the user to accomplish a task is called a user defined function.

Prerequisites:- Introduction to Function in C

Types of user defined functions in C

We can classify the basic function design by their return values and their parameters. Function either return a value or they don’t. It may have parameters or may not. Function with doesn’t return any value is called void functions. Combining return types and parameters in four basic designs:

1) Void functions with no parameter
2) Void functions with no parameters
3) Function with return value and no parameter
4) Function with return value and parameters

user defined functions in c

Void functions with no parameter

A void function may have no parameter. That function has the only task, displaying the message. That function doesn’t receive anything from the caller function and returns nothing back to the caller function.

#include<stdio.h>
float findarea(); //function declaration
int main()
{
   findarea(); //function calling
   return 0;
}
//function to find ara of rectangle
//function definition
float findarea()
{
   float length, width, area;
   printf("Enter length and width of rectangle: ");
   scanf("%f %f", &length, &width);
   area = length * width;
   printf("Area of rectangle = %.2f\n",area);
}

Output:-

Enter length and width of rectangle: 10 4.23
Area of rectangle = 42.30

The above program calculates the area of a rectangle. One void function findarea() is defined in the program and it doesn’t receive any data from the main function.

The function call still requires parentheses, however, even when no parameters are present. Without the parentheses, it is not a function call. Because a void function does not have a value, so it can be used only as a statement, It can’t be used as an expression.

In this type of function, there is no data communication between calling function and called function, as well as called function and caller function.

Void functions with parameters

#include<stdio.h>
void findarea(float, float); //function declaration
int main()
{
   float length, width;
   printf("Enter length and width of rectangle: ");
   scanf("%f %f", &length, &width);
   findarea(length, width); //function calling
   return 0;
}
//function to find ara of rectangle
//function definition
void findarea(float l, float b)
{
   float area;
   area = l * b;
   printf("Area of rectangle = %.2f\n",area);
}

Output:-

Enter length and width of rectangle: 12 4.3
Area of rectangle = 51.60

In this program, there is void function findarea() with parameters. The function receives two floating-point parameters. It is a void function, so it can be used only as a statement, It can’t be used as an expression.

Note that the names of the variables in the main function (length and width) and the name of parameters in function (l and b) may have the same names or different names, but their data type must be the same.

In this type of function, there is data communication between the calling function and called function but there is no data communication between the called function and calling function.

User defined Function in C with return value and no parameter

#include<stdio.h>
float findarea(); //function declaration
int main()
{
   float result;
   result = findarea(); //function calling
   printf("Area of rectangle = %.2f\n",result);
   return 0;
}
//function to find ara of rectangle
//function definition
float findarea()
{
   float length, width, area;
   printf("Enter length and width of rectangle: ");
   scanf("%f %f", &length, &width);
   area = length * width;
   return area;
}

Output:-

Enter length and width of rectangle: 10 3.5
Area of rectangle = 35.00

In this program, one non-void function findarea() is defined which doesn’t receive any data from the caller function and returns the area of the rectangle. The function findarea() takes input from the user, calculates the area and return the area to the caller function.

Some functions return a value but don’t have any parameters. The most common use for this design reads data from the keyword and returns the data to the calling program.

In this type of design, there is no communication between calling function and called function. But data communication exists between called function and calling function.

result = findarea();
In the main, the function call is evaluated first because it is a postfix expression, which has higher precedence than the assignment expression (=). To evaluate it, the function call is executed. Its value is the value returned by the called function. The expression value is then used in the binary expression (assignment). The side effect of the binary expression stores the returned value in the variable.

Functions that return a value can be used either in an expression statement or as an expression. When it is used as an expression statement, the return value is discarded. When it is used as an expression, as in the above program, its value is the value returned from the function.

User-defined Function in C with return value and parameters

Unlike the three designs discussed earlier, in this type two-way data communication takes place. That is, both the called and calling functions receive and transfer data from each other. Most of the time we used this approach.

#include<stdio.h>
float findarea(float, float); //function declaration
int main()
{
   float length, width, result;
   printf("Enter length and width of rectangle: ");
   scanf("%f %f", &length, &width);
   result = findarea(length, width); //function calling
   printf("Area of rectangle = %.2f\n",result);
   return 0;
}
//function to find ara of rectangle
//function definition
float findarea(float l, float b)
{
   float area;
   area = l * b;
   return area; //return statement
}

Output:-

Enter length and width of rectangle: 12.5 5
Area of rectangle = 62.50

In this program, one non-void function findarea() is defined which receives data from the caller function and, calculates the area, and returns the area to the caller function. Finally, the result is displayed on the screen in the main function.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply