The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C User Defined Functions

What Is User Defined Function

" What you have to do when I call you"
yes, the above quotes clearly define the purpose of user defined function. By speaking conceptually, a programmer can define their own sets of code inside a function and use it for n number of times using its function name.

C User Defined Functions

Types

The user-defined functions are classified into four types, according to parameters and return value.

  • Functions without arguments but with return values
  • Functions without arguments and without return values
  • Functions with arguments but without return values
  • Functions with arguments and without return values

#1 Functions Without Arguments Without Return Values

The calling function will not send parameters to the called function and called function will not pass the return value to the calling function.

C program - Functions Without Arguments Without Return Values

function1.c
#include <stdio.h>
void add();     //function declaration
int main()
{
//main function definition
add();    //function call without passing arguments
}
//function definition
void add()
{
int a = 5, b = 10;
int c;
printf(" The values of a and b : %d %d ", a, b);
c = a + b;
printf(" \nsum : %d ", c);
//no return values to the calling function
}
  • The values of a and b : 5 10
  • sum = 15

Note:

The program illustrates that, there are no parameters passed through the calling function and no return value to the calling function main().

#2 Functions With Arguments Without Return Values

The calling function will pass parameters to the called function but called function will not pass the return value to the calling function.

C program - Functions With Arguments Without Return Values

function2.c
#include <stdio.h>
void add(int,int);     //function declaration
int main()
{
//main function definition
int sum;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
add( a, b);    //function call with passing arguments to called function
}
// function definition
void add(int a, int b)
{
int c;
c = a + b;
printf(" \nsum : %d ", c);
//no return values to the calling function main
}
  • The values of a and b : 5 10
  • sum = 15

Note:

The program illustrates that, parameters are passed to the called function(add) from calling function(main) but no return values are passed from called function(add) to the calling function (main).

#3 Functions Without Arguments With Return Values

The calling function will not pass parameters to the called function but called function will pass the return value to the calling function.

C program - Functions Without Arguments With Return Values

function3.c
#include <stdio.h>
int add();     //function declaration
int main()
{
//main function definition
int sum;
sum = add();    //function call without passing arguments to called function
printf(" \nsum = %d ", sum);
}
// function definition
int add()
{
int c;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
c = a + b;
return c;    //passing return values to the calling function main
}
  • The values of a and b : 5 10
  • sum = 15

Note:

The program illustrates that, no parameters are passed to the called function(add) from calling function(main) but return value is passed from called function(add) to the calling function (main). The return values is assigned to the variable sum.

#4 Functions With Arguments With Return Values

The calling function will pass parameters to the called function and called function also pass the return value to the calling function.

C program - Functions With Arguments With Return Values

function4.c
#include <stdio.h>
int add(int, int);     //function declaration
int main()
{
//main function definition
int sum;
int a = 5, b = 10;
printf(" The values of a and b : %d %d ", a, b);
sum = add( a, b);    //function call with passing arguments
printf(" \nsum = %d ", sum);
}
// function definition
int add(int a, int b)
{
int c;
c = a + b;
return c;    //passing return values to the calling function main
}
  • The values of a and b : 5 10
  • sum = 15

Note:

The program illustrates that, parameters are passed to the called function (add) from calling function (main) and return value is passed from called function (add) to the calling function (main). The return values is assigned to the variable sum.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report