Reading: Chapter 7
A function is a way of isolating some code in a smaller module. They're useful for two primary reasons: When you have some code that you want to use in many places (it's much better to use functions than to copy-and-paste --- it makes it easier to modify the program). And they're useful when you have a very large program, you can use functions to break the program down into smaller, more manageable pieces.
A function takes parameters. It processes them and produces a return value in response.
To use a function, you type the function name, followed by the value of the parameters in parentheses. This can be a part of any expression.
C++ compilers include a myriad of pre-defined functions useful functions. You can use them by including new #include statements. Here is an example of a program that uses the pow() function, which given two arguments x and y returns xy.
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
int main() {
double x;
cout << "Find cube root of what? ";
cin >> x;
cout << "Cube root = " << pow(x, 1.0/3.0) << endl;
return 0;
}
[a list of many more useful functions]
In your programs, you will want to define new functions so that you can use them many times. The following example illustrates this.
#include <iostream>
#include <string>
using namespace std;
int fact(int what) {
int ret = 1;
int i;
for(i = 1; i <= what; i = i + 1) {
ret = ret * i;
}
return ret;
}
int main() {
int n;
int r;
cout << "Choose how many of how many? ";
cin >> r >> n;
cout << (fact(n) / fact(r) / fact(n - r)) << " choices" << endl;
return 0;
}
It's important to remember that functions cannot see variables defined in other functions. Thus any information that the function needs should be passed via the parameter list.
Usually in C++, a parameter is passed by value. That is, changes to the parameter variable do not affect variables outside the function.
// setToZero
#include <iostream>
#include <string>
using namespace std;
void setToZero(int n) {
n = 0;
return;
}
int main() {
int i = 1;
setToZero(i);
cout << i << endl;
return 0;
}
When setToZero() changes n, this does not affect
i since n just has a copy of i's value
before the function. So the printed value is 1.
You can tell C++ to use pass parameters by reference instead so that (in the above function call) n refers to the same box as i. This is just a matter of adding a single ampersand.
void setToZero(int &n) {
If we do this, the program will print 0 instead.
A final alternative parameter-passing scheme is passing parameters by constant reference. Here the compiler passes the parameter by reference but does not allow you to change them. The following does this.
void setToZero(const int &n) {
The compiler will not even allow this to compile now.
You may wonder why constant-reference parameters are useful. They're useful when you want to pass a large variable (like an entire array) by value but you want to guarantee the rest of the program that the function won't touch things. By passing by reference you avoid the necessity of having to copy the entire array, something that is quite time-consuming, while you still keep the safety guarantee of call-by-value.