Professional Documents
Culture Documents
Types of Functions in C
In real time, a function may be defined with or without parameters and a function
may or may not return a value. It is completely depends upon the user
requirement. In this article, We will explain you the types of functions in C
Programming language with example.
NOTE:
From the above, 1 and 3 types does not return any value when the function is
called so, We use void return type while defining the function.
When we call the function 2 and 4 types will return some value so, We have to
use the appropriate data type (int, float, double etc) as return type while
defining the function. We use return keyword inside the function to return
some value when the function is called from the main() function or any sub
functions.
In this method, We won’t pass any arguments to the function while defining,
declaring or calling the function. This type of functions will not return any value
when we call the function from main() or any sub function. When we are not
C Programming Java R SQL MySQL Python BI Tools About
expecting any return value but, we need some statements to be printed as output
then, this type of functions are very useful.
In this program, We are going to calculate the Sum of 2 integer values and print
the output from the user defined function itself.
CODE
OUTPUT
https://www.tutorialgateway.org/types-of-functions-in-c/ 2/12
6/22/2018 Types of Functions in C Programming
ANALYSIS
If you observe the main() function, We haven’t passed any arguments /parameters
to the function Addition()
1 Sum = a + b
2 = 10 + 20
3 = 30
Whenever we call the Addition () function then it will print the same output because
the values of a and b are fixed inside the function.
https://www.tutorialgateway.org/types-of-functions-in-c/ 3/12
6/22/2018 Types of Functions in C Programming
CODE
1 #include<stdio.h>
2
3 int Multiplication();
4
5 int main()
6 {
7 int Multi;
8
9 Multi = Multiplication();
10 printf("\n Multiplication of a and b is = %d \n", Multi );
11
12 return 0;
13 }
14
15 int Multiplication()
16 {
17 int Multi, a = 20, b = 40;
18
19 Multi = a * b;
20
21 return Multi;
22 }
OUTPUT
https://www.tutorialgateway.org/types-of-functions-in-c/ 4/12
6/22/2018 Types of Functions in C Programming
ANALYSIS
We declared the integer variable Multi and we assigned it to the return value of the
Multiplication () function because, user defined function also return integer value
only.
1 Multi = Multiplication();
Whenever we call the Multiplication () function then it will print the above
statement.
https://www.tutorialgateway.org/types-of-functions-in-c/ 5/12
6/22/2018 Types of Functions in C Programming
1 Multi = a * b
2 = 20 * 40
C Programming
3 = 800 Java R SQL MySQL Python BI Tools About
Whenever we call the Multiplication () function then it will print the same output
because the values of a and b are fixed inside the function.
This method 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 our user to pass his own data to the function arguments but we
are not expecting any return value then, this type of functions are very useful.
This program allows the user to enter 2 integer values and then, We are going to
pass those values to the user defined function to calculate the sum.
CODE
1 #include<stdio.h>
2
3 void Addition(int, int);
4
5 void main()
6 {
7 int a, b;
8
9 printf("\n Please Enter two integer values \n");
10 scanf("%d %d",&a, &b);
11
12 //Calling the function with dynamic values
https://www.tutorialgateway.org/types-of-functions-in-c/ 6/12
6/22/2018 Types of Functions in C Programming
13 Addition(a, b);
14 }
15
16 void Addition(int a, int b)
17 {
18 int Sum;
19 C Programming Java R SQL MySQL Python BI Tools About
20 Sum = a + b;
21
22 printf("\n Additiontion of %d and %d is = %d \n", a, b, Sum);
23 }
OUTPUT
ANALYSIS
If you observe the main() function, We declared the integer variables a and b.
https://www.tutorialgateway.org/types-of-functions-in-c/ 7/12
6/22/2018 Types of Functions in C Programming
In the next line, we called the Addition (int a, int b) function with the user entered
values
C Programming
1 Addition(a, b); Java R SQL MySQL Python BI Tools About
We declared the integer variables of Sum and also we have integer (a, b)
arguments in the function. This means, this function will allow the user to pass 2
integer values.
This type of user defined functions are called as fully dynamic function means, it
provide maximum control to the end user.
This program allows the user to enter 2 integer values and then, We are going to
pass those values to the user defined function to multiply those values and return
the value using return keyword.
CODE
1 #include<stdio.h>
2
https://www.tutorialgateway.org/types-of-functions-in-c/ 8/12
6/22/2018 Types of Functions in C Programming
OUTPUT
ANALYSIS
https://www.tutorialgateway.org/types-of-functions-in-c/ 9/12
6/22/2018 Types of Functions in C Programming
If you observe the main() function, We declared the integer variables Multi, a and b.
In the next line, we assigned the return value of the Multiplication(int a, int b)
function to the Multi integer because, user defined function also return integer
value only.
We declared the integer variables of Multi and also we have two integer (a, b)
arguments in the function. This means, this function will allow the user to pass 2
integer values.
https://www.tutorialgateway.org/types-of-functions-in-c/ 10/12
6/22/2018 Types of Functions in C Programming
TRENDING
https://www.tutorialgateway.org/types-of-functions-in-c/ 11/12
6/22/2018 Types of Functions in C Programming
First Name
Last Name
E-Mail Address
GO
https://www.tutorialgateway.org/types-of-functions-in-c/ 12/12