Function with no argument and no Return value Function with no argument and with a Return value Function with argument and No Return value Function with argument and Return value
As per our requirement, We can define the User-defined functions in multiple ways. The Following examples will explain you the available function types in C programming.
This type of functions can either be used to display information or they are completely dependent on user inputs.
In these functions, We won’t pass any arguments to the function while defining, declaring or calling the function. This type of functions in C will not return any value when we call the function from main() or any sub function. When we are not expecting any return value but, we need some statements to be printed as output then, this type of functions in C are very useful.
Example 1 : Find greater number form 2 numbers entered by user
#include<stdio.h>
void greatNum(); // function declaration
int main()
{
greatNum(); // function call
return 0;
}
void greatNum() // function definition
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
if(i > j) {
printf("The greater number is: %d", i);
}
else {
printf("The greater number is: %d", j);
}
}
Example 2 : Calculate the Sum of 2 integer values and print the output
#include<stdio.h>
// Function Declaration
void Addition();
void main()
{
printf("\n ............. \n");
Addition();
}
void Addition()
{
int Sum, a = 10, b = 20;
Sum = a + b;
printf("\n Sum of a = %d and b = %d is = %d", a, b, Sum);
This type of functions can also either be used to display information or they are completely dependent on user inputs. In addition returned value can be used in programme further.
In these functions, We won’t pass any arguments to the function while defining, declaring or calling the function. This type of functions will return some value when we call the function from main() or any subfunction.
The Data Type of the return value will depend upon the return type of function declaration. For instance, if the return type is int then return value will be int.
Example 1 : Find and return greater number form 2 numbers entered by user
#include<stdio.h>
int greatNum(); // function declaration
int main()
{
int result;
result = greatNum(); // function call
printf("The greater number is: %d", result);
return 0;
}
int greatNum() // function definition
{
int i, j, greaterNum;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
if(i > j) {
greaterNum = i;
}
else {
greaterNum = j;
}
// returning the result
return greaterNum;
}
Example 2 : Calculate and return the Sum of 2 integer values and print the output
#include<stdio.h>
// Function Declaration
void Addition();
void main()
{
printf("\n ............. \n");
int Sum= Addition();
printf("\n Sum of a and b is = %d", Sum);
}
void Addition()
{
int Sum, a = 10, b = 20;
Sum = a + b;
return Sum;
}
This type of functions allows us to pass the arguments to the function while calling the function. But, This type of functions will not return any value when we call the function from main () or any sub function.
If we want to allow user to pass data to the function arguments but we are not expecting any return value then, this type of functions are very useful.
Example 1: Find greater number form 2 numbers entered by user
#include<stdio.h>
void greatNum(int a, int b); // function declaration
int main()
{
int i, j;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
greatNum(i, j); // function call
return 0;
}
void greatNum(int x, int y) // function definition
{
if(x > y) {
printf("The greater number is: %d", x);
}
else {
printf("The greater number is: %d", y);
}
}
Example 2 : Calculate the Sum of 2 integer values and print the output
#include<stdio.h>
void Addition(int, int);
void main()
{
int a, b;
printf("\n Please Enter two integer values \n");
scanf("%d %d",&a, &b);
//Calling the function with dynamic values
Addition(a, b);
}
void Addition(int a, int b)
{
int Sum;
Sum = a + b;
printf("\n Additiontion of %d and %d is = %d \n", a, b, Sum);
}
Function with argument and Return value
This is the best type, as this makes the function completely independent of inputs and outputs, and only the logic is defined inside the function body.
This function allows us to pass the arguments to the function while calling the function. This type of functions will return some value when we call the function from main () or any sub function. Data Type of the return value will depend upon the return type of function declaration. For instance, if the return type is int then return value will be int.
Example 1: Find greater number form 2 numbers entered by user
#include<stdio.h>
int greatNum(int a, int b); // function declaration
int main()
{
int i, j, result;
printf("Enter 2 numbers that you want to compare...");
scanf("%d%d", &i, &j);
result = greatNum(i, j); // function call
printf("The greater number is: %d", result);
return 0;
}
int greatNum(int x, int y) // function definition
{
if(x > y) {
return x;
}
else {
return y;
}
}
Example 2 : Calculate the Sum of 2 integer values and print the output
#include<stdio.h>
void Addition(int, int);
void main()
{
int a, b,Sum;
printf("\n Please Enter two integer values \n");
scanf("%d %d",&a, &b);
//Calling the function with dynamic values
Sum=Addition(a, b);
printf("\n Additiontion of %d and %d is = %d \n", a, b, Sum);
}
int Addition(int a, int b)
{
int Sum;
Sum = a + b;
Return Sum;
}