@don
Well your function had a return type of void, and none of the arguments were pointers or references, so the function will have no effect on the value of variables in
main()
.
Had you made the
tem
argument a reference, it would have changed
tempf
in
main()
.
This is a key concept about functions: what goes on inside them is separate to everything else. The parameters are copies of the variables that exist when the function is called. An obvious way to communicate an "answer" is to return a value.
Btw, arguments are the variables or literal values that functions are called with, parameters are the variables a function receives.
The other way with using a reference or pointer is provided because it is better than passing a large data structure by value. In other words, instead of copying the whole large data structure, just send a pointer or reference to it instead. Prefer to use references instead of pointers, because they normally have to refer to a variable (unless they go out of scope), whereas a pointer can be made to point at anything - including garbage or null.
Here are some simple examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#include <iostream>
void AddTwo(int MyNumber);
int main() {
int MyNumber =3;
int Answer = 0;
AddTwo(MyNumber);
std::cout << "Answer is " << Answer << "\n"; // Prints Answer is 0
return 0;
}
void AddTwo(int MyNumber) {
int Answer = 0; // A local variable, not the same as Answer in main
Answer = MyNumber + 2; // no effect in main()
}
|
Here is the same thing again, this time returning a value:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <iostream>
int AddTwo(int MyNumber);
int main() {
int MyNumber =3;
int Answer = 0;
Answer = AddTwo(MyNumber);
std::cout << "Answer is " << Answer << "\n"; // Prints Answer is 5, this is what we want
return 0;
}
int AddTwo(int MyNumber) {
int Answer = 0; // A local variable, not the same as Answer in main
Answer = MyNumber + 2; // no effect in main()
return Answer; // send the result back to main
}
|
This time using a reference for a big data container:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <vector>
// function decalarations in same order as called if possible
void AddNumber(std::vector<int>& BigVector, const int MyNumber);
void PrintVector(const std::vector<int>& BigVector);
int main() {
int MyNumber = 3; // declare 1 variable per line, use meaningful names, comment if need be
std::vector<int> BigVector(15, 0); // 15 elements initialised to 0, imagine it is 1000 elements
BigVector = {1,2,3,4,5,6,7,8,9,10};
AddNumber(BigVector, MyNumber);
PrintVector(BigVector);
return 0;
}
// function definitions in same order as declared
// name the parameters the same as the arguments to reinforce the idea that they are the same
// BigVector is a reference, will change values in main()
void AddNumber(std::vector<int>& BigVector, const int MyNumber) {
// put comments here to describe what the function does plus
// any preconditions like range of acceptable values etc.
// range based for loop - good for processing all elements - easier than iterators
// needs c++11 for this
for (auto &item : BigVector) { // auto detects type, could have used int
item += MyNumber;
}
}
void PrintVector(const std::vector<int>& BigVector) /*const*/ { // if this was a class function it would be const
for (auto &item : BigVector) {
std::cout << item << "\n";
}
}
|
So, some important things at this stage: Naming of variables & functions; proper formatting & indenting of code; understand return values, references & local variables for functions.
Some other stuff to read:
Most important:
Google & wiki are your best friends !! :+D
EDIT:
I probably shouldn't have bothered to make
item
a reference here, in the :
for (auto &item : BigVector)
Because it is only an int, had it have been a more complicated type - then a ref would have been worthwhile.
However it needs to be a ref in the
AddNumber
function so it can change the values.