Explore

Edit

Edit

Types of user defined functions in C

Published on 06 Mar 2020

Different types of user-defined functions: A function, depending on whether arguments are present or not and whether a value is returned or not, may belong to one of the following categories.

• Category I: Functions with no arguments and no return values
• Category 2: Functions with no arguments and with return values
• Category 3: Functions with arguments and no return values
• Category 4: Functions with arguments and with return values

1) Functions with no arguments and no return value:

The following syntax describes how to write a function which neither takes any arguments nor returns any value.

Syntax for function definition

void functionName()     //Return type in the function header should be 'void' and do not give any arguments within the parenthesis.
{
........
........
return;                                     //Need not return any value
}


Syntax for function call

functionName();

Example: Write a function to increase the value of the given variable by 10

As arguments should not be passed to the function and no value should be returned, the function body should be written in such a way that it reads the input value, increases its value by 10 and prints the result within the function itself.

include <stdio.h>
void modify()                   //function definition
{
int a;
printf("Enter a value:");
scanf("%d",&a);
a=a+10;
printf("The modified value is %d",a);
return;
}

int main()
{
modify();                           //function call
getch();
return 0;
}

output: if a=1
The modified value is 11.


2) Functions with no arguments and with return values:

The following syntax shows how to write a function which does not take any arguments but returns some value back to the calling function.

Syntax for function definition

returnType functionName()     //Identify what type of value will be returned by the function and accordingly specify the return type and empty
{                                                                      parenthesis since there is no argument
. . . .
. . . .
return value;
}


Syntax for function call

X = functionName     //As the function is going to return the modified Value, call the function like this.

Example: The previous program is modified such that the value of 'a' is read in the user-defined function and the changed value is returned to the main(), where it gets printed.

include <stdio.h>
void modify()
{
int a;
printf("Enter a value:" )
scanf("%d",&a);
a=a+10;
return a;
}

int main()
{
int x;
x=modify();
printf("The modified value is %d",x);
getch();
return 0;
}


Assume a=1. When a=a+10' is executed a=11. Here the modified value (11) is returned to the place where the function was called. 11 is returned as x=11. If x is printed, 11 will be the output.

3) Functions with arguments and no return values:

Syntax for function definition

void functionName(typel argl, type2 arg2,...... typeN argN)
{
. . . .
. . . .
return;
}


Syntax for function call

functionName(argl, arg2,       , argN);

Example: In the above program, read the value of 'a' in main(), pass it to modify() and print the changed value of 'a' in modify() function itself.

include <stdio.h>
void modify(int x)                // function takes the argument x and since it does not return anything, specify 'void'
{
x = x + 10;
printf("The modified value is %d", x);
return;
}

int main()
{
int a;
printf("Enter a value:");
scanf("%d",&a);
modify(a);                                // pass the argument 'a' to the function
getch();
return 0;
}


a's value is read in the main function and that value is passed to the modify() function. a's value is assigned to the formal argument 'x'. Then 'x' value is changed and printed in the modify() function itself.

4) Functions with arguments and with return values:

Syntax for function definition

returnType functionName(typel argi, type2 arg2,.........typeN argN)
{
. . . .
. . . .
return value;
}


Syntax for function call

X = functionName(argl, arg2,... , argN);

﻿Example: In the above program, read the value of 'a' in main(), pass it to modify() and return the changed value of 'a' to main() and print the changed value in main().

include <stdio.h>
void modify(int x)     // function takes the argument x and since it does not return anything, specify 'void'
{
x = x + 10;
return x;
}

int main()
{
int a;
printf("Enter a value:");
scanf("%d",&a);
printf("the modified value is %d", modify(a));   // pass the argument 'a' to the function
getch();
return 0;
}

`