Pages

Monday, December 7, 2009

Function with arguments

What are arguments ?
Functions can also be categorized on the basis of arguments. Argument or parameter is the value that we pass to a function while calling it. for example while calling the printf(), we always the message that we want to print on the screen, like printf(“Hi there !!”). The value or message written inside the circular brackets is called the argument. Of course, not all functions accept values inside like clrscr(),getch() etc. such functions are called functions without arguments.


Why do we need arguments?

Imagine a program that wants to perform addition,subtraction,product and division on a single pair of values. if we create four different functions for this purpose one each for addition, subtraction and product, we will have to write the code to get the input from the user in each function since we cannot pass values from a function to another. Consider the following example

void main()


{


void add();


void product();


void subtraction();


void division();


add();


product();


subtraction();


division();


}


void add()


{


int a,b,c;


printf(“\nEnter first number “);


scanf(“%d”,&a);


printf(“\nEnter second number “);


scanf(“%d”,&b);


c=a+b;


printf(“\nAddition is %d”,c);


}


void subtraction()


{


int a,b,c;


printf(“\nEnter first number “);


scanf(“%d”,&a);


printf(“\nEnter second number “);


scanf(“%d”,&b);


c=a-b;


printf(“\nSubtraction is %d”,c);


}


void product()


{


int a,b,c;


printf(“\nEnter first number “);


scanf(“%d”,&a);


printf(“\nEnter second number “);


scanf(“%d”,&b);


c=a*b;


printf(“\nProduct is %d”,c);


}


void division()


{


int a,b,c;


printf(“\nEnter first number “);


scanf(“%d”,&a);


printf(“\nEnter second number “);


scanf(“%d”,&b);


c=a/b;


printf(“\nDivison is %d”,c);


}



As you can see, we have to take input in each function causing the program to lengthen. We cannot move the values of “a” and “b” from one function to another since they are all local variables. The solution to this problem is the use of arguments. Arguments or parameters can be moved to another function so that we don’t have to input the values again and again.

Example : Using arguments


void main()

{


void add(int,int);


void product(int,int);


void subtraction(int,int);


void division(int,int);


int a,b,c;


printf(“\nEnter first number “);


scanf(“%d”,&a);


printf(“\nEnter second number “);


scanf(“%d”,&b);






add(a,b);


product(a,b);


subtraction(a,b);


division(a,b);


}


void add(int a,int b)


{


int c;


c=a+b;


printf(“\nAddition is %d”,c);


}


void subtraction(int x,int y)


{


int z;


z=x-y;


printf(“\nSubtraction is %d”,z);


}


void product(int i,int j)


{


int k;


k=i*j;


printf(“\nProduct is %d”,k);


}


void division(int m,int n)


{


int o;


o=m/n;


printf(“\nDivison is %d”,o);


}

Description

After accepting values from the user and saving them in a and b the first call is made to the add(). Notice that inside the circular brackets the values a and b are written (a ,b). This is called passing arguments to a function – in this case a and b are arguments while “add” is the name of the function. When the compiler sees that a call is made to add() it will copy the values of a and b defined in main() to the arguments a and b given in the definition of add(). Which means that the value of a will be copied to a and b will be copied to b. inside the add (), c will add both the values and the result is printed. After this the second function subtraction is called using the same arguments (a and b) but this time their value are copied to x and y which are defined in the subtraction () definition. Similarly values of and b are copied to I and j in product() and to m and n in division. The main benefit of using arguments in this program is that we don’t have to ask the user to enter values in all the functions. The values have been entered just once and are copied to all the functions using arguments.

Points to remember

1. The argument given while calling the function is called actual arguments, while the arguments given in the function definition are called formal arguments.

2. The names of the formal and actual arguments can be same or different.





Example : Calculating area of circle using functions with arguments






void main()


{


clrscr(); // to clear all previous messages on the screen (optional)


void area(int r); // prototyping the function with the name area and one integer arg.


int radius;


printf(“\nEnter Radius Of The Circle : “);


scanf(“%d”,&radius);


area(radius);//calling the function with user defined radius as argument


area(4); // calling the function with a constant


}


void area(int rad)// the value of radius will be copied to rad


{


float ar;


ar=3.14*r*r; // calculating area of circle using the formula pi * radius * radius


printf(“\nArea of circle is %f”,ar);


}



Description

As you can see the function is declared to accept an integer value as argument and therefore must be given while calling it. Before calling the function for the first time, the user is prompted to enter a value as radius. After this the function is called with the statement “area(radius)”. At this point the value of the actual argument “radius” is copied to the formal argument “rad” declared in the function definition. The area function calculates the area using the mathematical formula “3.14*radius*radius”. When the area is printed the control returns to the calling function (which is main()) which again calls the function with a constant value of 4. Please note that the function need an integer value to run. Whether this value is being provided by the user as in the first case or if it is a fixed value like in the second case is immaterial. The function definition does not care how the argument is passed – by the user or as a fixed value – as long as the method of passing the value is correct.



Example : Calling a function with arguments passed in four ways



void main()


{


void si(int,float,float);


int p;


float r,t;


printf(“\nEnter Principle Amount : “);


scanf(“%d”,&p);


printf(“\nEnter Rate Of Interest : “);


scanf(“%f”,&r);


printf(“\nEnter Time Duration : “);


scanf(“%f”,&t);


si(p,r,t);// function call with user values


si(4000,5.3,3.2); // function call with constant values


si(p,6.4,4);// calling the function with a user defined value and two constants


si(4000+2000,r,t*3); // calling the function with different type of arguments.


}






void si(int princ,float rate,float time)


{


float s=(princ*rate*time)/100;


printf(“\nSimple Interest is %f”,s);


}



Description

In this example a function named “si” is declared to calculate simple interest. The function has three arguments one integer and two floats. The function has been called four time 1) With the values entered by the user 2) With fixed values 3) With a combination of user entered values and constants 4) With a formula result, user entered values and again a formula result.

As exaplained earlier the function has nothing to do with the way the arguments are passed to it. It will only check whether the number of arguments is correct and whether the datatype of arguments is matching or not. If these things are correct it will calulcate and print the result.

0 comments:

Post a Comment