Header Ads Widget

Ticker

6/recent/ticker-posts

Types of function

 4.6. Types of function:

4.6.1. Types of function based on return type and argument list:

There are four types of C functions:

1. C function with arguments and with return value or return type:

2. C function with arguments and without return value or return type:

3. C function without arguments and with return value or return type:

4. C function without arguments and without return value or return type:


Sr.no

Types of C functions

syntax

1

with arguments and with return type

int function ( int ); // function declaration

function ( a ); // function call

int function( int a ) // function definition

{statements; return value;}

 

2

with arguments and without return type

void function ( int ); // function declaration

function( a ); // function call

void function( int a ) // function definition

{statements;}

 

3

without arguments and without return type

void function(); // function declaration

function(); // function call

void function() // function definition

{statements;}

 

4

without arguments and with return type     

int function ( ); // function declaration

function ( ); // function call

int function( ) // function definition

{statements; return value;}

 

 



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.


4.6.1.1. C function with arguments and with return value or return type:

Here function having both return type as well as argument list is shown in following program.

Program: Design a function which takes two integer values and make addition and return addition to calling function.

#include< stdio.h >

#include< conio.h >

void main ()

{

int a,b,c;

int sum (int ,int); /only mentioning data type is also allowed in function prototype/

printf(“\n Enter two number ”);

scanf(“%d %d ”& a, &b);

c=sum (a, b);

printf(“\n Addition is =%d ”,c);

}

int sum (int a, int b)

{

int c;

c=a + b;

return c; /*returning ‘c’ value which is of integer data type so data type of sum() is integer */

}

Output:

Enter two number

21

31

Addition is =52


4.6.1.2. C function with arguments and without return value or return type:

Here function having only argument list but no return type that return type is void, shown in following program. Writing void as a return type when function dose not return any thing is mandatory. Otherwise return type of a function is int by default.

Program: Design a function sum, which takes two integer values and make addition and does not return addition to calling function. But sum function itself print addition into its body.

#include< stdio.h >

#include< conio.h >

void main ()

{

int a,b,c;

void sum (int ,int); /only mentioning data type is also allowed in function prototype/

printf(“\n Enter two number ”);

scanf(“%d %d ”& a, &b);

sum (a, b);

getch();

}

void sum (int a, int b)

{

int c;

c=a + b;

printf(“\n Addition is =%d ”,c);

}

Output:

Enter two number

21

31

Addition is =52


4.6.1.3. C function without arguments and with return value or return type:

Here function having only return type but no argument list that is argument list is void, shown in following program. Writing void in argument list is optional.

Program: Design a function sum, which takes two integer values inside the body of sum function and make addition and return addition to calling function. And calling function print the addition of two numbers. Sum function does not take any argument from the calling function. Here in below program, sum is called function and main is calling function.

#include< stdio.h >

#include< conio.h >

void main ()

{

int c;

int sum ();

c= sum ();

printf(“\n Addition is =%d ”,c);

getch();

}

int sum ()

{

int a, b;

printf(“\n Enter two number ”);

scanf(“%d %d ”& a, &b);

c=a + b;

return c;

}

Output:

Enter two number

21

31

Addition is =52


4.6.1.4. C function without arguments and without return value or return type:

Here function does not have return type and no argument list that is argument list is void, also return type is also void shown in following program. Writing void in argument list is optional. But when function dose not return anything then must write return type as void.

Program: Design a function sum, which takes two integer values inside the body of sum function and make addition and dose not return addition to calling function. But sum function itself print addition inside the body of sum function.

#include< stdio.h >

#include< conio.h >

void main ()

{

int c;

void sum (void);

sum ();

getch();

}

void sum (void)

{

int a, b;

printf(“\n Enter two number ”);

scanf(“%d %d ”& a, &b);

c=a + b;

printf(“\n Addition is =%d ”,c);

}

Output:

Enter two number

21

31

Addition is =52


4.5.2. Types of function based on how to pass arguments to a function:

1. Call by value:

2. Call by reference:

4.5.2.1. Call by value:

In call by value parameters or arguments are passed by their value. When we pass parameter by value then its new copy is created in called function. When we made some changes in that copied value then that change is not reflected in original copy in calling function. That is old copy contains previous value but new copy get changed value which is not reflected in old copy. That is old copy remains same and only new copy get changed.

Program: Swap the two numbers using call by value.

#include< stdio.h >

#include< conio.h >

void swap(int x, int y)

{

int t;

t=x;

x=y;

y=t;

}

void main()

{

int a, b;

printf(“\n Enter the first number a=”);

scanf(“%d”, &a);

printf(“\n Enter the second number b=”)

; scanf(“%d ”,&b);

swap(a , b); //actual swapping is not done in call by value

printf(“\n After swapping value of a = %d”, a);

printf(“\n After swapping value of b = %d ”, b);

getch();

}

Output:

Enter the first number a= 10

Enter the second number b= 20

After swapping value of a = 10

After swapping value of b = 20

Explanation:

In call by value when a and b argument is passed to function swap, that values are copied to x and y. When we made changes on x and y that is only visible into swap function and not reflected back into a and b in main function. Old values that is a and b remains same only x and y values get swapped. So output after swapping remains same in the main function.


4.5.2.2. Call by reference:

In call by reference parameters or arguments are passed by their reference or address. In call by reference new copy of argument is not created only address of particular argument is passed. So when the changes are made in called function then those changes get reflected in calling function also. That is changes are reflected in old copy itself but changed values are accessed to both called and calling function.

Program:Swap the two numbers using call by reference.

#include< stdio.h >

#include< conio.h >

void swap(int * x, int *y)

{

int t;

t = *x;

*x = *y;

*y = t;

}

void main()

{

int a, b;

printf(“\n Enter the first number a=”);

scanf(“%d”, &a);

printf(“\n Enter the second number b=”);

scanf(“%d ”,&b);

swap(&a , &b); //actual swapping is done in call by reference

printf(“\n After swapping value of a = %d”, a);

printf(“\n After swapping value of b = %d ”, b);

getch();

}

Output:

Enter the first number a= 10

Enter the second number b= 20

After swapping value of a = 20

After swapping value of b = 10

Explanation:

In call by reference, address of variable a and b is passed to the function swap. When we swap values in swap function is reflected in old values a and b. So after swapping a becomes 20 and b becomes 10 in main function.


4.5.3. Types of functions based on recursion:

1. Recursive functions:

2. Non-recursive functions:

4.5.3.1. Recursive functions:

Recursive function is a function that is calling to itself.

Two types:

1. Direct recursive function

2. Indirect recursive function

Function that calls itself in the body of that same function is called direct recursion.

Function A is said to be indirect recursion if it calls another function B which in turn calls A function.

Program: Implementation of factorial function using recursion.

#include< stdio.h >

#include< conio.h >

long factorial(int);

void main()

{

int n;

long f;

printf("Enter an integer to find its factorial\n");

scanf("%d", &n);

if (n < 0)

printf("Factorial of negative integers isn't defined.\n");

else

{

f = factorial(n);

printf("%d! = %ld\n", n, f);

}

getch();

}

long factorial(int n)

{

if (n == 0)

return 1;

else

return(n * factorial(n-1));

}

Output:

Enter an integer to find its factorial

4

4! = 24


4.5.3.2. Non-recursive functions:

Non recursive function is a function that does not call to itself in the body of a function.

Program: Implementation of factorial function using non-recursion.

#include < stdio.h >

#include< conio.h >

long factorial(int);

void main()

{

int number;

long fact = 1;

printf("Enter a number to calculate its factorial\n");

scanf("%d", &number);

printf("%d! = %ld\n", number, factorial(number));

getch();

}

long factorial(int n)

{

int c;

long fact = 1;

for (c = 1; c <= n; c++)

{

fact = fact * c;

}

return fact;

}

Output:

Enter a number to calculate its factorial

4

4! = 24


4.5.4. Types of function based on built-in or user defined:

4.5.4.1. Built-in or predefined or library functions:

Built in functions are those functions whose definitions are already written in the header files. For example, printf(), getch(), clrscr() all these functions are built-in or predefined functions.

4.5.4.2. User defined function:

User defined functions are those functions whose definitions are written by programmer itself. For example sum or sumdiff or swap functions written above are the user defined function.

Post a Comment

0 Comments