In this tutorial, we will learn about different approaches of user defined function. where you can solve the same problem by four different ways using funcitons.
- Example: Function with no argument and no return value
- Example: Function with argument but no return value
- Example: Function with no argument but return value
- Example: Function with argument and a return value
- Learn more about function
Quick links
There can be 4 different types of user defined function as given below:
- Function with no argument and no return value
- Function with argument but no return value
- Function with no argument but return value
- Function with argument and a return value
A signal program can be defined by 4 different ways as mentioned above. Let's understand with a simple example to find sum of two integers input given by user.
Example 1: Function with no argument and no return value
/* Program two find sum of two integers input given by user */
#include <stdio.h>
#include <conio.h>
/*Function declaration OR function prototype*/
void sum(void);
void main()
{
clrscr();
/* Function call with no argument */
sum();
getch();
}
/* Creating user defined function OR function definition*/
void sum()
{
int n1, n2, result;
printf("Enter two numbers: ");
scanf("%d%d", &n1, &n2);
result = n1 +n2;
printf("Sum = %d", result);
}
Here, sum(); function is called from the main() with no arguments. Since, no value is returned from the function. Therefore, return type of sum() will be void.
Note: The empty parentheses in sum(); statement inside the main() function indicates that no arguments is passed to the function.
Output
Enter two numbers: 10, 20
sum = 30
Example 2: Function with argument but no return value
/* Program two find sum of two integers input given by user */
#include <stdio.h>
#include <conio.h>
/*Function declaration OR function prototype*/
void sum(int, int);
void main()
{
int a, b;
clrscr();
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
/* Function call with argument */
sum(a, b);
getch();
}
/* Creating user defined function OR function definition*/
void sum(int n1, int n2)
{
int result;
result = n1 +n2;
Printf("Sum = %d", result);
}
The value of a and b entered by the user is passed to the sum(a, b) function, and this value will be copied to the formal parameters.
Here, variables n1 and n2 will be replaced with the value of a and b in sum(int n1, int n2); function.
Remember: When we pass the actual parameters while calling a function then this is known as function call by value. I this case the values of actual parameters are copied to the formal parameters.
Output
Enter two numbers: 10 20
sum = 30
Example 3: Function with no argument but return value
/* Program two find sum of two integers input given by user */
#include <stdio.h>
#include <conio.h>
/*Function declaration OR function prototype*/
int sum(void);
void main()
{
int s;
clrscr();
/* Function call with no argument */
s = sum();
Printf("Sum = %d", s);
getch();
}
/* Creating user defined function OR function definition*/
int sum()
{
int n1, n2, result;
printf("Enter two numbers: ");
scanf("%d%d", &n1, &n2);
result = n1 +n2;
return result;
}
Output
Enter two numbers: 10 20
sum = 30
Example 4: Function with argument and a return value
/* Program two find sum of two integers input given by user */
#include <stdio.h>
#include <conio.h>
/*Function declaration OR function prototype*/
int sum(int, int);
void main()
{
int a, b, s;
clrscr();
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
/* Function call with argument */
s = sum(a, b);
Printf("Sum = %d", s);
getch();
}
/* Creating user defined function OR function definition*/
int sum(int n1, int n2)
{
int result;
result = n1 +n2;
return result;
}
Output
Enter two numbers: 10 20
sum = 30
- Function in C
- Properties of function
- User defined function
- Function call by value
- Function call by reference
- Library function
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.