Write the syntax for the declaration of a function. Also discuss the parameter passing methods with an example program. 10m Dec2007

By | June 9, 2015

Explanation of FUNCTION:

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.

Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by itself. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind.

The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location within a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.

We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.

 

Recursive function is a function which calls itself. Calling a function within itself makes it a endless loop. So we need to take care that there must be a termination condition in every recursive function.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

[codesyntax lang=”c”]

int factorial(int);

[/codesyntax]

Function returns value and the ones which does not return any value is called and declared as void. This function which we are writing returns the factorial in decimal form as output. The values inside the round brackets are called Arguments. The Argument and return type are integer type. A function can return only one value so we have to take only one data type in which its value is expected.

Let’s discuss the parameter passing methods with an example program of Factorial program.

First variables will be the one which will save the value entered by the user NUM. Other variable will be FACT which will be for holding the resultant factorial value returned by the FUNCTION. The identified variables are FACT,NUM.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

Main program needs only declaration of variables and function prototype. Also messages for user and scanning values  NUM and FACT is used to display the resultant values returned by the FUNCTION.

[codesyntax lang=”c”]

#include<stdio.h>
void main()
{
 int factorial(int);
 int FACT,NUM;
 printf("ENTER NUMBER : ");
 scanf("%d",&NUM);
 FACT=factorial(NUM);
 printf("\nFACTORIAL OF GIVEN NUMBER IS %d ",FACT);
 getch();
}

[/codesyntax]

Let’s identify variables needed for function which is part of program outside main function.
Variables which are passed in the function may not be declared again which is N.  NUM is passed to the function and saved in N variable of the function. Now the variable RESULT which will hold the resultant value to be returned by the FUNCTION. Variable.  The identified variable is RESULT.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

[codesyntax lang=”c”]

int RESULT;

[/codesyntax]

In this program there is a risk of generating an ENDLESS LOOP. Here we have to Check the TERMINATION condition is given or not. N is the given value it is reduced by one in the recursive function call as we have to multiply all the number from given N to 1. Their must stop at N equals 1, and if user enters value 1 then the result is also 1, hence we should return 1 in if condition and recursion in else condition. Return the Result at the end.

[codesyntax lang=”c”]

int factorial(int N)
{
 int RESULT;
    if(N==1)
    return(1);
    else
    RESULT=N*factorial(N-1);
 return(RESULT);
}

[/codesyntax]

Steps of function call :-

RESULT=0   & N=5, RESULT = 5 * factorial(4), hence RESULT=5
RESULT=5   & N=4, RESULT = 4 * factorial(3), hence RESULT=20
RESULT=20  & N=3, RESULT = 3 * factorial(2), hence RESULT=60
RESULT=60  & N=2, RESULT = 2 * factorial(1), hence RESULT=120
RESULT=120  & N=1, RESULT = 1 * 1, hence RESULT=120

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable.

C program code :

[codesyntax lang=”c” lines=”normal”]

#include<stdio.h>
void main()
{
 int factorial(int);
 int FACT,NUM;
 printf("ENTER NUMBER : ");
 scanf("%d",&NUM);
 FACT=factorial(NUM);
 printf("\nFACTORIAL OF GIVEN NUMBER IS %d ",FACT);
 getch();
}

int factorial(int N)
{
 int RESULT;
    if(N==1)
    return(1);
    else
    RESULT=N*factorial(N-1);
 return(RESULT);
}

[/codesyntax]

Screen Shots :-

C_program_Factorial_Recursion

C_program_Factorial_Recursion_Output

Write a recursive function in ‘C’ that computes the factorial of a given integer. 10m Dec2007

Solved program can be found on this link http://cssimplified.com/c-programming/a-c-program-to-find-the-factorial-of-a-number-using-recursion