Industrial Training

Return values and their types



All C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Now, we will see simple example C programs for each one of the below.

  1. C function with arguments (parameters) and with return value
  2. C function with arguments (parameters) and without return value
  3. C function without arguments (parameters) and without return value
  4. C function without arguments (parameters) and with return value
S.no C function syntax
1 with arguments and with
return values
int function ( int ); // function declaration
function ( a ); // function call
int function( int a ) // function definition
{statements; return a;}
2 with arguments and without
return values
void function ( int ); // function declaration
function( a ); // function call
void function( int a ) // function definition
{statements;}
3 without arguments and without
return values
void function(); // function declaration
function(); // function call
void function() // function definition
{statements;}
4 without arguments and with
return values
int function ( ); // function declaration
function ( ); // function call
int function( ) // function definition
{statements; return a;}

Note:

  • If the return data type of a function is “void”, then, it can’t return any values to the calling function.
  • If the return data type of the function is other than void such as “int, float, double etc”, then, it can return values to the calling function.

1. Example program for with arguments & with return value:

In this program, integer, array and string are passed as arguments to the function. The return type of this function is “int” and value of the variable “a” is returned from the function. The values for array and string are modified inside the function itself.

#include<stdio.h>
#include<string.h>

int function(int, int[], char[]);

int main()
{
      int i, a = 20;
      int arr[5] = {10,20,30,40,50};  
      char str[30] = "\"fresh2refresh\"";

      printf("    ***values before modification***\n");  
      printf("value of a is %d\n",a);  

      for (i=0;i<5;i++)
      {
         // Accessing each variable
         printf("value of arr[%d] is %d\n",i,arr[i]);  
      }
      printf("value of str is %s\n",str); 

      printf("\n    ***values after modification***\n"); 

      a= function(a, &arr[0], &str[0]);

      printf("value of a is %d\n",a);  

      for (i=0;i<5;i++)
      {
         // Accessing each variable
         printf("value of arr[%d] is %d\n",i,arr[i]);  
      }
      printf("value of str is %s\n",str); 

      return 0;
}

int function(int a, int *arr, char *str)
{
    int i;

    a = a+20;
    arr[0] = arr[0]+50;
    arr[1] = arr[1]+50;
    arr[2] = arr[2]+50;
    arr[3] = arr[3]+50;
    arr[4] = arr[4]+50;
    strcpy(str,"\"modified string\"");

    return a;

}

Output:

***values before modification***
value of a is 20
value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50
value of str is “fresh2refresh”***values after modification***
value of a is 40
value of arr[0] is 60
value of arr[1] is 70
value of arr[2] is 80
value of arr[3] is 90
value of arr[4] is 100
value of str is “modified string”

2. Example program for with arguments & without return value:

In this program, integer, array and string are passed as arguments to the function. The return type of this function is “void” and no values can be returned from the function. All the values of integer, array and string are manipulated and displayed inside the function itself.

#include<stdio.h>

void function(int, int[], char[]);

int main()
{
      int a = 20;
      int arr[5] = {10,20,30,40,50};  
      char str[30] = "\"fresh2refresh\"";

      function(a, &arr[0], &str[0]);
      return 0;
}

void function(int a, int *arr, char *str)
{
    int i;

    printf("value of a is %d\n\n",a);  

      for (i=0;i<5;i++)
      {
         // Accessing each variable
         printf("value of arr[%d] is %d\n",i,arr[i]);  
      }
    printf("\nvalue of str is %s\n",str);  
}

Output:

value of a is 20

value of arr[0] is 10
value of arr[1] is 20
value of arr[2] is 30
value of arr[3] is 40
value of arr[4] is 50

value of str is “fresh2refresh”

3. Example program for without arguments & without return value:

In this program, no values are passed to the function “test” and no values are returned from this function to main function.

#include<stdio.h>

void test();          

int main()
{
    test();                         
    return 0;
}

void test()  
{ 
       int a = 50, b = 80;   
       printf("\nvalues : a = %d and b = %d", a, b);
}

Output:

values : a = 50 and b = 80

4. Example program for without arguments & with return value:

In this program, no arguments are passed to the function “sum”. But, values are returned from this function to main function. Values of the variable a and b are summed up in the function “sum” and the sum of these value is returned to the main function.

#include<stdio.h>

int sum();          

int main()
{
    int addition;
    addition = sum();                  
    printf("\nSum of two given values = %d", addition);
    return 0;
}

int sum()  
{ 
       int a = 50, b = 80, sum;   

       sum = a + b;
       return sum;       
}

Output:

Sum of two given values = 130

Do you know how many values can be return from C functions?

  • Always, Only one value can be returned from a function.
  • If you try to return more than one values from a function, only one value will be returned that appears at the right most place of the return statement.
  • For example, if you use “return a,b,c” in your function, value for c only will be returned and values a, b won’t be returned to the program.
  • In case, if you want to return more than one values, pointers can be used to directly change the values in address instead of returning those values to the function.
Hi I am Pluto.