Loading....
Coupon Accepted Successfully!

 

Formal Arguments

The arguments specify in function header is called as formal arguments because they represent the names of data items that are transferred in to the function from the calling portion of the program. They are also called as formal parameters


Actual Arguments:

The arguments specify in function call is called as actual arguments. Since they define the data items that are actually transferred. They are also called as actual parameters or simply arguments.

 

Example:

main ()
{
int a, b, c;
printf ("\n Enter two number");
scanf ("\%d %d ", &a ,&b);
Actual arguments
c = add (a, b); Function Call
printf("The addition of two no is %d ",c)
Formal arguments
Add (int x, inty)
{
int z;
z = x +y;
return (z);
}

 

Division

Program

#include

int add (int x, int y) //A
{
int z; //B
z = x + y;
return (z); //C
}
main ()
{
int i, j, k;

i = 10;
j = 20;
k = add(i, j); //D
printf ("The value of k is%d\n", k); //E
}

 

Explanation

  • This function adds two integers and returns their sum.
  • When defining the name of the function, its return data type and parameters must also be defined. For example, when you write
  • int add (int x, int y)
  • int is the type of data to be returned, add is the name of the function, and x and y are the parameters of the type int. These are called formal parameters.
  • The body of a function is just like the body of main. That means you can have variable declarations and executable statements.
  • A function should contain statements that return values compatible with the function's return type.
  • The variables within the function are called local variables.
  • After executing the return statement, no further statements in the function body are executed.
  • The name of the function can come from the arguments that are compatible with the formal parameters, as indicated in statement D.
  • The arguments that are used in the call of the function are called actual parameters.
  • During the call, the value of the actual parameter is copied into the formal parameter and the function body is executed.
  • After the return statement, control returns to the next statement which is after the call of the function.

function call

Functions cannot execute themselves with exception to main (). In a C program only main is executable. All other functions are not. If this is true then there must be some way by which the functions executes. This is known as function call. A function can be called from another function with exception to main() function. This means main() functions can call other functions but other functions can not call main(). If by curiosity you happen to call main() function and it compiled well then you may end up in infinite loop.

A function is called from within another function by just putting its name inside that function with proper parameters and if it returns any thing then you must supply a proper variable to catch the value. Before you can call the function it should be defined or at least prototyped. See the example bellow.

 

/*ihis function takes 2 integer and return their sum*/

#include
int add(int a , int b)
{
int c;
c=a+b;
return ( c);
}
void main()
{
int k;
printf(“Main function calling add()\n”);
k=add(5,10);
printf(“Return value from called function is %d\n”,k);
}

 

In the above code we first define a function add(), then we call it from function main() with 2 arguments 3 and 4. We catch the returned value in which is an integer type, next we print the value stored in k. From above code it is clear that the calling function should supply the arguments to called function and store the returned value if any from the called function. Just like main() function in above program any other function can call function add. To test that just patch the above code as follows

#include

int add(int a , int b)
{
int c;
c=c+b;
}
float calculate(int d, int e, int f); //prototype declaration of function
main()
{
int k,l,m;
float p;
printf("Enter 3 integers k I ");

scanf("%d%d%d", &k&l&m);
printf ("Now calling clculte () \ n");
p=calculate (k, I m); //calling of function
printf ("The return value from calculate () is %f\ n",p);
}
float calculate(int d, int e, int f) //definition of function
{
int g;
float h;
g=add(d, e);
h=1.0*g/e;
return(h);
}

 

Parameter passing

Information can be passed from one function to another using parameters.

Program

 

main ( )

{
int i;
i = 0;
printf (" The value of i before call %d \n", i);
f1 (i);
printf (" The value of i after call %d \n", i);
}
void f1 (int k)
{
k = k + 10;
}

 

Explanation
  • The parameter used for writing the function is called the formal parameter, k in this case.
  • The argument used for calling the function is called the actual parameter.
  • The actual and formal parameters may have the same name.
  • When the function is called, the value of the actual parameter is copied into the formal parameter. Thus k gets the value 0. This method is called parameter passing by value.
  • Since only the value of i is passed to the formal parameter k, and k is changed within the function, the changes are done in k and the value of i remains unaffected.
  • Thus i will equal 0 after the call; the value of i before and after the function call remains the same.

Categories of function

A Function in C belongs to one of the following categories:

  • Function with no arguments and no return values
  • Function with arguments and no return values
  • Function with no arguments and return values
  • Function with arguments and return values
  • Function with no arguments and no return values

When a function has no actual arguments, it does not receive any data from the calling function. Similarly, when any function does not return any value, the calling function does not receive any value from the function

 

Example:

main ()
{
add ();
}
Add()
{
int a, b, c;
printf ("\n Enter two numbers");
scanf ("\%d %d ", &a, &b);
c = a +b;
printf ("The addition of two no is %d",c)
return (0);
}

 

Function with arguments and no return values

In this category a function has arguments, i.e. the function receives data from the calling function. In this category the actual arguments and formal arguments should match in number, type and order. The values of actual arguments are assigned to the formal arguments on a one to one basis and it does not return any values to the calling function

 

Example:
main ( )
{
int a, b;
printf ("\n Enter two number");
scanf ("\%d %d ", &a ,&b);
Add (a, b);
}
Add (int x, int y)
{
int z;
z = x +y;
printf("The addition of two no is %d ",z);
return (0);
}

 

Function with no arguments and return values

In this category a function does not receive any value from calling function but it returns

values to the calling function.

 

Example:

main ()
{
int a;
a = Add ();
printf(xThe addition of two no is %d ",a); }
Add()
{
int a, b,c;
printf ("\n Enter two numbers");
scanf ("\%d %d * &a ,&b);
c = a +b;
return (c);
}

 

Function with arguments and return values

In this category a function receives values from calling function and it also returns values to the calling function.

Example:
main ()
{
int a, b,c;
printf ("\n Enter two numbers");
scanf ("\%d %d ", &a ,&b);
c = add (a,b);
printf("The addition of two no is %d ",c);
Add (int x, inty)
{
int z; z = x +y;
return (z);
}

 

Function prototype

Any function by default returns an int value. If we desire that a function should return a value other than an int, then it is necessary to explicitly mention so in the calling functions as well as in the called function, e.g

main ()

{
float a,b,
printf ("\n Enter any number");
scanf("%d, &a );
b = square (a);
printf ("\n square of % f is % f', a,b);
}
square (float x)
{
float y;
y = x * x;
return (y);
}

 

*--------------Output of this program------------*

Enter any number 2.5

square of 2.5 is 6.000000

Division

 

 
Note:
Here 6 is not a square of 2.5.The reason is any C function by default always returns.

 

 

an integer value

main ( )
{
float square ( );

float a, b;
printf ("\n Enter any number");
scanf ("%f' &a);
b = square (a);

printf ("\n square of % f is % f,"a,b);
}
float square (float x)
{
float y;
y= x *x;
return ( y);
}

 

*--------------------Output------------------*

Enter any number 2.5

Square of 2.5 is 6.2500000





Test Your Skills Now!
Take a Quiz now
Reviewer Name