Types of Functions in C Explained with Example
A function in C programming is a piece of code that completes a particular task. Functions are used to divide a programme into more manageable, smaller chunks. A program's readability and maintainability are both enhanced by functions.
There are two types of functions in C:
-
Built-in functions: These are functions that are already defined in the C library, such as printf() and scanf(). They are provided by the C language and can be called directly in a program.
-
User-defined functions: These are functions that are created by the user. They are written by the programmer to perform specific tasks and can be called throughout the program.
There are three types of User-defined functions in C:
-
No Return, No Argument
-
No Return, with Argument
-
With return, with Argument
No Return , No Argument
Functions with no return and no arguments are also referred to as void functions in C. Both the input and the output of these functions are empty. They are used to perform particular operations or tasks, like printing a message or setting up variables.
A no return, no argument function in C has the following syntax:
void function_name()
{
// function body
}
Here, "void" keyword is used to specify that the function does not return any value. The function name is followed by a pair of empty parentheses, which indicate that the function does not take any arguments.
Example:
#include <stdio.h>
void add(); //Declaration
int main()
{
add(); //Calling
return 0;
}
void add() // Definition
{
int a, b,sum;
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
sum = a + b;
printf("The sum of %d and %d is: %d", a, b, sum);
}
In this example, the function "print_hello()" does not take any input and does not return any value. It only prints the message "Hello World!" when called. The function print_hello will be executed and "Hello World!" will be printed on the screen.
No Return,With Argument
No return with argument functions in C are also known as void functions. These functions take input in the form of arguments, but do not return any value. They are used to perform specific tasks or operations, such as printing a message or initializing variables, with the provided inputs.
The syntax for a no return with argument function in C is as follows:
void function_name(data_type arg1, data_type arg2, ...)
{
// function body
}
Here, "void" keyword is used to specify that the function does not return any value. The function name is followed by a pair of parentheses containing the list of arguments, which indicate that the function takes inputs. The data type of each argument is specified before the argument name.
Here is an example of a C program that uses a no return with argument function to perform the addition of two numbers:
#include <stdio.h>
void add(int,int); //Declaration
int main()
{
int a, b;
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
add(a, b); //Calling
return 0;
}
void add(int num1, int num2) //Definition
{
int sum = num1 + num2;
printf("The sum of %d and %d is: %d", num1, num2, sum);
}
In this program, the function "add()" is a no return with argument function that takes two integer arguments, "num1" and "num2", and performs the addition of these two numbers. The function then uses the printf() function to print the sum of the numbers on the screen.
With Return,With Argument
With return with argument functions in C are also known as non-void functions. These functions take input in the form of arguments and return a value. They are used to perform specific tasks or calculations, such as performing mathematical operations or string manipulations, and return the result of the operation.
The syntax for a with return with argument function in C is as follows:
data_type function_name(data_type arg1, data_type arg2, ...)
{
// function body
return value;
}
Here, "data_type" is used to specify the return type of the function. The function name is followed by a pair of parentheses containing the list of arguments, which indicate that the function takes inputs. The data type of each argument is specified before the argument name. The return statement is used to return the value from the function to the calling function.
Example of with Return and With Argument is given below.
#include<stdio.h>
int add(int,int); //Declaration
int main()
{
int a, b, result;
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
result = add(a, b); //Calling
printf("The sum of %d and %d is: %d", a, b, result);
return 0;
}
int add(int num1, int num2)
{
int sum;
sum= num1 + num2;
return sum;
}
In this example, the function "add()" takes two integer arguments, "num1" and "num2", and returns the sum of these two numbers. The function add is called with the input from the user, and the result is stored in the variable "result", this variable is then used to print the sum of a and b.