next up previous
Next: Functions Returning Values Up: Functions Previous: Functions

Function Arguments

It is possible to pass one or more values to a function. A value passed to a function is called an argument. Lets consider the following program.

Example
// use of arguments in functions

#include <iostream.h>
void mul(int x, int y); // prototype
main()
{
mul(10,20); // the function mul() is called. x=10 and y=20 are passed to the function mul()
mul(5,6); //the function mul() is called. x=5 and y=6 are passed to the function mul()
return 0;
}
void mul(int x, int y)
{
cout << x*y << " ";
}

This program will print 200 and 30 on the screen. When mul() is called, the C++ compiler copies the value of each argument into the matching parameter. That is, in the first call to mul() 10 is copied into x and 20 is copied into y. In the second call 5 is copied into x and 6 into y.

When you create a function that takes one or more arguments the variables that will receive those arguments must also be declared. These variables are called the parameters of the function.



Yousef Haik
2/23/1998