Tutorial Gateway

  • C Language
  • Java
  • R
  • SQL
  • MySQL
  • Python
  • BI Tools
    • Informatica
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • QlikView
  • Js

Types of Functions in C

by suresh

In real time, a function may be defined with or without parameters and a function may or may not return a value. It completely depends upon the user requirement. In this article, We will explain you the types of functions in C Programming language with an example.

In C Programming, As per our requirement, We can define the User-defined functions in multiple ways. The following are list of available types of Functions in C

  1. Function with no argument and no Return value
  2. Function with no argument and with a 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.

C Function with No argument and No Return value

In this method, 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.

C 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.

/* Function with No argument and No Return value Example */
#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);
}

OUTPUT

Types of Functions in C Programming 1

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 the sum, a, b and we assigned 10 to a and 20 to b.

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

Sum = a + b
    = 10 + 20
    = 30

Below printf statement is used to print the output.

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.

C 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. 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.

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.

#include<stdio.h>

int Multiplication();        

int main()
{
  int Multi;

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

  return 0;            
}

int Multiplication()
{
  int Multi, a = 20, b = 40;  
  
  Multi = a * b;

  return Multi;
}

OUTPUT

Types of Functions in C Programming 2

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 returns integer value only.

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

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.

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

Multi = a * b
      = 20 * 40
      = 800

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.

C 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 subfunction.

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.

#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);
}

OUTPUT

Types of Functions in C Programming 4

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.

printf("\n Please Enter two integer values \n");
scanf("%d %d",&a, &b);

In the next line, we called the Addition (int a, int b) function with the user entered values

Addition(a, b);

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

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

C 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 subfunction. 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 a fully dynamic function means, it provides 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.

#include<stdio.h>

int Multiplication(int, int);        

int main()
{
  int a, b, Multi;

  printf("\n Please Enter two integer values \n");
  scanf("%d %d",&a, &b);

  //Calling the function with dynamic values
  Multi = Multiplication(a, b);

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

int Multiplication(int a, int b)
{
  int Multi;  

  Multi = a * b;

  return Multi;
}

OUTPUT

Types of Functions in C Programming 5

ANALYSIS

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.

printf("\n Please Enter two integer values \n");
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 returns integer value only.

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

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 integers (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

Stay in Touch!

Sign Up to receive Email updates on Latest Tutorials

  • C Programs
  • Java Programs
  • SQL FAQ’s
  • Python Programs
  • SSIS
  • Tableau
  • JavaScript

Copyright © 2019 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy