Functions (cont)
When a return statement is executed, the function terminates:
- the returned
expression will be evaluated
- all local variables and parameters will be thrown away when the function terminates
- the calling function is free to use the returned value, or to ignore it
Example:
// Euclid's gcd algorithm (recursive version)
int euclid_gcd(int m, int n) {
if (n == 0) {
return m;
} else {
return euclid_gcd(n, m % n);
}
}
|
The return statement can also be used to terminate a function of return-type void :
|