A function is written in C++ when there’s a certain chunk of code that will be executed more than once, or maybe even just to separate a certain chunk of code from the rest of the code. For example, to convert degrees Celsius to degrees Fahrenheit:
//Sample code for functions in C++ float CtoF(float val) { return val *= 1.8 += 32; } float FtoC(float val) { return (val -= 32) /= 1.8; } int main() { float temperature = float(); //temperature now is 0, temperature == 0 temperature = 37.5; CtoF(temperature); //temperature is still equal to 37.5 temperature = 0; FtoC(temperature); //temperature is still equal to 0 CtoF(temperature); //temperature == 0 }
Note that neither of the two functions, CtoF and FtoC, change the value of the variable temperature in main. temperature has been passed “by value”. What this means is that CtoF and FtoC get their own local variable, which is a copy of the one supplied as an argument to the function. As such, val is a copy of temperature. When a change is made to val, no change at all whatsoever is made to temperature – they are separate variables.
What if you do want the function to change the variable supplied as an argument? Here’s how:
void ChangeFromCtoF(float& ref) { return ref *= 1.8 += 32; } int main() { float temperature(34.0); ChangeFromCtoF(temperature); //temperature == 93.2 }
Now in the above, the variable temperature is changed because it is passed “by reference”. A reference is just another name for another variable. ref in ChangeFromCtoF is just another name for temperature in main. You can define a reference wherever you want, just like a normal variable:
int main()
{
int a;
int& b = a;
a = 5;
b += 1;
//a == 6
//b == 6
}
In the above, a and b refer to the same variable. By defining the reference b, I have just given a variable another name. If you don’t like the name of a variable, give it another!:
signed main() { int some_stupid_unbelievably_long_name = 5; int& k = some_stupid_unbelievably_long_name; k = 2; ? //some_stupid_unbelievably_long_name == 2 //k == 2 }
As you may have already guessed, a reference must be initialized when defining it, just as a const variable must be initialized when defined. The following won’t compile:
signed int main()
{
int& a;
//the following is also illegal
int& b = 5;
//because 5 isn’t a variable
}
Now, I’m going to talk about function declarations. Consider the following code:
int main() { Blah(); } void Blah() { }
The above won’t compile. Why? Here’s why: the compiler comes along and reads the file from top to bottom. It walks through main, comes to Blah and it says, “I’ve never heard of Blah, COMPILE ERROR”. There are two options: put Blah before main, or… put in a function declaration:
extern void Blah(); int main() { Blah(); } void Blah() { }
Now when the compiler comes to Blah in the function main, it already knows that it’s a function and it knows its return value and what type of arguments it takes. In the above, the extern keyword is optional. extern means “defined somewhere else”. The reason why it’s optional is that if it’s taken out, the intention is still obvious – ?a function is being declared which is defined somewhere else.
Variables in C++ functions:
When a variable is defined within a function, the variable is created when the statement of its definition is reached. The variable is destroyed when the end of the function is reached, most likely at a return statement. These are called local variables.
If you define a variable outside of a function, then what you’ve got is a global variable. A global variable is created when the program starts and is destroyed when the program ends. It can be accessed by any function in the program:
Whether a variable is local or global is referred to as the “scope” of the variable.
int j; void Blah(); int main() { j = 5; Blah(); void Blah() { j = 6; }
A variable can be declared static in a function, as so:
void Chimney() { unsigned counter = 0; ++counter; }
In the above, the counter variable is not destroyed when the end of the function is reached. Instead, once defined for the first time, ie. the first time the function is called, it stays around for the rest of the program. As such, the next time the function is called, the variable will contain the same value. A good example of this is a variable that counts how many times the function has been called. Note that a static variable is initialized only once. As a result, it doesn’t get set to 0 the next time the function is called!