C – Argument, return value

Prev     Next

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.
C functions aspects
syntax
1. With arguments and with
return values
function declaration:
int function ( int );

function call: function ( a );


function definition:       
int function( int a )
{
statements;
return a;
}

2. With arguments and without
return values
function declaration:
void function ( int );

function call: function( a );

function definition:
void function( int a )
{
statements;
}

3. Without arguments and without
return values
function declaration:
void function();


function call: function();

function definition:
void function()
{
statements;
}

4. Without arguments and with
return values
function declaration:
int function ( );

function call: function ( );

function definition:
int function( )
{
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.

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.

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.

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.

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.

Continue on C – Library functions….

Continue on C – User defined functions & adding them in C library….

Continue on C – Command line arguments….

Continue on C – Variable length arguments….

Prev     Next





Share this website !!!
Facebook Twitter Linkedin