| Previous | Home | Next |
If a function is to use arguments, it must declare variables that accept the values of the arguments. While calling a function, there are two ways that arguments can be passed to a function:
- Call by value
- Call by reference
# Call by value
Whenever we called a function and passed something to it we have always passed the 'Values' of variables to the called function. Such function is called Call By Value.
Defining a Function: Consider the fuction factorial()
/* Factorial of a number */
int factorial(int x)
{
int f=1,i; /* local variable declaration */
for(i=x; i>=1; i--)
f=f*i;
return(f);
}
Now, let us call the function factorial() by passing actual values as in the following example:
#include <stdio.h>
#include <conio.h>
/* function declaration */
int factorial (int);
void main
{
int a, fact; /* local variable declaration */
clrscr();
printf("\n Enter any number");
scanf ("%d",&a);/* calling a function of factorial of a number */
fact=fatorial(a); /* local variable declaration */
printf("factorial value=%d",fact);
}
/* Factorial of a number */
int factorial(int x)
{
int f=1,i; /* local variable declaration */
for(i=x; i>=1; i--)
f=f*i;
return(f);
}
Output : Enter any number: 5
factorial value=120
| Previous | Home | Next |