You are on page 1of 12

6/22/2018 Types of Functions in C Programming

C Programming Java R SQL MySQL Python BI Tools About

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.

In C Programming, As per our requirement, We can define the User defined


functions in multiple ways

1. Function with no argument and no Return value


2. Function with no argument and with Return value
3. Function with argument and No Return value
4. Function with argument and Return value

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.

Types of Functions in C Programming


The Following examples will explain you the available function types in C
programming.

Function with No argument and No Return value


https://www.tutorialgateway.org/types-of-functions-in-c/ 1/12
6/22/2018 Types of Functions in C Programming

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.

Function with No argument and No Return value Example

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

1 /* Function with No argument and No Return value Example */


2 #include<stdio.h>
3  
4 // Function Declaration
5 void Addition();        
6  
7 void main()
8 {
9   printf("\n ............. \n");
10   
11   Addition();                      
12 }
13  
14 void Addition()
15 {
16   int Sum, a = 10, b = 20;  
17   Sum = a + b;
18   
19   printf("\n Sum of a = %d and b = %d is = %d", a, b, Sum);
20 }

OUTPUT

https://www.tutorialgateway.org/types-of-functions-in-c/ 2/12
6/22/2018 Types of Functions in C Programming

C Programming Java R SQL MySQL Python BI Tools About

ANALYSIS

If you observe the main() function, We haven’t passed any arguments /parameters
to the function Addition()

Within the Addition ()function,

We declared the integer variables of sum, a, b and we assigned 10 to a and 20 to


b.

In the next line we calculate the sum using Arithmetic operator ( + )

1 Sum = a + b
2     = 10 + 20
3     = 30

Below printf statement is used to print the output.

1 printf("\n Sum of a = %d and b = %d is = %d", a, b, Sum);

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

Function with no argument and with Return value


In this method, We won’t pass any arguments to the function while defining,
declaring or calling the function.
C Programming Java This
R type
SQLof functions
MySQL will Python
return some value when
BI Tools About
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.

Function with No arguments and with Return value Example

In this program, We are going to calculate the multiplication of 2 integer values


using the user defined function without arguments and return keyword.

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

C Programming Java R SQL MySQL Python BI Tools About

ANALYSIS

If you observe the main() function,

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();

We haven’t passed any arguments /parameters to the function Multiplication()

In the next line, printf statement is used to print the output

1 printf("\n Multiplication of a and b is = %d \n", Multi);

Whenever we call the Multiplication () function then it will print the above
statement.

Within the Multiplication () function,

We declared the integer variables of Multi, a, b and we assigned 20 to a and 40 to


b.

https://www.tutorialgateway.org/types-of-functions-in-c/ 5/12
6/22/2018 Types of Functions in C Programming

In the next line we Multiplied both a and b using Arithmetic operator ( * )

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.

Function with argument and No Return value


If you observe the above 2 methods, No matter how many times you executive, it
will give the same output. We don’t have any control over the values of the
variables a and b because they are fixed values. In real time, we mostly deal with
dynamic data means we have to allow the user to enter his own values rather than
fixed ones.

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.

Function with argument and No Return value Example

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.

This program allows the user to enter 2 integer values as a and b.

1 printf("\n Please Enter two integer values \n");


2 scanf("%d %d",&a, &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

Within the Addition (int a, int b) function,

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.

In the next line we added both a and b using Arithmetic operator ( + )

In the next line, printf statement is used to print the output

1 printf("\n Additiontion of %d and %d is = %d \n", a, b, Sum);

Function with argument and Return value


This method 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.

This type of user defined functions are called as fully dynamic function means, it
provide maximum control to the end user.

Function with arguments and Return value Example

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

3 int Multiplication(int, int);        


4  
5 int main()
6 {
7   int a, b, Multi;
8  
9 C Programming
  printf("\n Java
Please Enter R
two integer SQL \n");
values MySQL Python BI Tools About
10   scanf("%d %d",&a, &b);
11  
12   //Calling the function with dynamic values
13   Multi = Multiplication(a, b);
14  
15   printf("\n Multiplication of %d and %d is = %d \n", a, b, Multi);        
16   return 0;            
17 }
18  
19 int Multiplication(int a, int b)
20 {
21   int Multi;  
22  
23   Multi = a * b;
24  
25   return Multi;
26 }

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.

This program allows the user to enter 2 integer values as a and b.


C Programming Java R SQL MySQL Python BI Tools About
1 printf("\n Please Enter two integer values \n");
2 scanf("%d %d",&a, &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.

1 Multi = Multiplication(a, b);

We passed the user inputs as the arguments /parameters to the function


Multiplication(int a, int b)

In the next line, printf statement is used to print the output

1 printf("\n Multiplication of a and b is = %d \n", Multi);

Within the Multiplication () function,

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.

In the next line we Multiplied both a and b using Arithmetic operator ( * )

Thank you for visiting Our Blog

Placed Under: C Programming

Share your Feedback, or Code!!

https://www.tutorialgateway.org/types-of-functions-in-c/ 10/12
6/22/2018 Types of Functions in C Programming

C Programming Java R SQL MySQL Python BI Tools About

TRENDING

SQL Server Integration Services


(SSIS)
SQL Server Tutorial
SQL Server Reporting Services
(SSRS)
Tableau Tutorial
Home
C Program to Print Prime Numbers
from 1 to 100
C Programming Language

https://www.tutorialgateway.org/types-of-functions-in-c/ 11/12
6/22/2018 Types of Functions in C Programming

Search For Tutorials

C Programming Java R SQL MySQL Python BI Tools About


STAY IN TOUCH!

Sign Up to receive Email updates


on Latest Tutorials

First Name

Last Name

E-Mail Address

GO

BLOGGER C PROGRAMS JAVA PROGRAMS SQL SSIS

SSRS TABLEAU JAVASCRIPT

COPYRIGHT © 2018 | TUTORIAL GATEWAY· ALL RIGHTS RESERVED BY SURESH


HOME | ABOUT US | CONTACT US | PRIVACY POLICY

https://www.tutorialgateway.org/types-of-functions-in-c/ 12/12

You might also like