|
||
|
|
| Learn to program in C++ | ||||||
| Posted September 9, 2006. Written by John Zervos, author of the text Byte Into C++ (2002). Tertiary Press: Melbourne. | ||||||
| This
article provides a comprehensive yet simple to understand treatment of the
C++ language for beginners. The article is divided into a series of
separate lessons as follows:
Lesson 1 - Introduction |
||||||
|
| #include <iostream> using std::cout; using std::endl; void greeting(); // Function prototype int main() { greeting(); // Function call system("pause"); return 0; } void greeting() // Function definition { cout << "Welcome!" << endl; } |
Passing arguments (parameters) to a function
When a function is called, it is possible to pass data (referred to as arguments or parameters) to the function for processing. You can pass more than one parameter at a time, and the parameters can be of any primitive or user-defined type. The parameter list is placed within the function's brackets. Parameters can be passed to a function by value or by reference. Both methods are outlined below.
Passing arguments by value
When an argument is passed to a function by value, a copy of the original data is made available to the function. Any change to the copy within the function does not affect the original data value. This type of parameter passing is more common in pure C programs. Consider the following function definition:
void square(int y)
{
cout << y*y << endl;
}
When the square function is called, a parameter is passed to it by value and is assigned to the variable y. This variable is local to the function and cannot be accessed outside the scope of that function.
The function prototype will be the same as the first line of the function terminated with a semicolon as shown:
void square(int y);
A possible call to the function may look something like this:
int x = 2;
square(x); // Function call
Here, the parameter x, having the value 2, is passed to the function. Notice that the function call simply consists of the function name with the parameter list in brackets.
The complete program is shown below:
| #include <iostream> using std::cout; using std::endl; void square(int y); // Function prototype int main() { int x = 2; square(x); // Function call system("pause"); return 0; } void square(int y) // Function definition { cout << y*y << endl; } |
In this program, an integer variable x is declared and initialised with the value 2. The program then calls the square function and passes it a copy of the parameter's value, 2. In the function, the passed value 2 is assigned to the local variable y, which is then squared and displayed.
Passing arguments by reference
When an argument is passed to a function by reference, a type of pointer to the original data is passed to the function, rather than a copy of the data. This is a more resource efficient way of passing data to a function but it means that the function can modify the original data (which can be both a good thing and a bad thing).
Consider the following function definition:
void square(int& y)
{
y = y*y;
}
The reference operator, &, tells the compiler that parameter y will be a reference to the original data passed to the function. In other words, y becomes an alias (another name for) the original data passed to the function. If the value of y is modified, so too will the original data.
When the square function is called, a parameter is passed to it by reference and is assigned to the variable y. This variable is local to the function and cannot be accessed outside the scope of that function, however, it "points to" the original data in the calling function. The statement y = y*y in the function squares the value of y, and so too squares the original data in the calling function.
The function prototype will be the same as the first line of the function terminated with a semicolon as shown:
void square(int& y);
A possible call to the function may look something like this:
int x = 2;
square(x); // Function call
cout << x << endl;
Here, the parameter x, having the value 2, is passed to the function by reference because of the & operator used in the function header and prototype.
The complete program is shown below:
| #include <iostream> using std::cout; using std::endl; void square(int& y); // Function prototype int main() { int x = 2; square(x); // Function call cout << x << endl; system("pause"); return 0; } void square(int& y) // Function definition { y = y*y; } |
It's also possible to pass parameters to a function by reference for efficiency, but make them "read only" so that they can't be modified by the function. This is done by using the const keyword in the local variable declaration as shown:
void square(const int& y)
{
}
Passing multiple arguments
It's possible to pass multiple arguments to a function of different data types and using a mix of pass by value and pass by reference. An example is shown below:
void details(const int& age, char gender, float& weight)
{
}
Returning by value
A function is able to return a single value on termination. Returning values in this way is popular in C, but with pass-by-reference in C++, this feature is largely redundant. Nevertheless, consider the following function definition:
int getAge()
{
int age;
cout << "Please enter your age: ";
cin >> age;
return age;
}
In this function, a local integer variable age is declared. The user is prompted for their age and its value assigned to the variable age. The return keyword both terminates the function and passes a copy of age's value back to the calling program. The function is of type int, rather than void, to denote the type of data being returned.
The function prototype will be the same as the first line of the function terminated with a semicolon as shown:
int getAge();
A simple call to the function may look something like this:
getAge(); // Function call
However, this is inadequate because the returned value, in this case the user's entered age, is lost. Therefore, the function call can be used in assignment statement as shown below, so that the returned value is assigned to a variable for later use.
int userAge;
userAge = getAge(); // Function call
Alternatively, you could combine the function call with a cout statement to immediately display the user's entered age as shown below:
cout << getAge(); // Function call
This statement performs the dual role of calling the function and sending its returned value to the output stream.
Functions that return values can also take parameters by value and/or reference.
| © 2006 learn2dostuff.com |