So this seems like a fairly simple question, but I haven't been able to find an answer to it anywhere online. The reason I'm assuming a function which returns a variable is less efficient than a function which takes a variable and changes it is because I think the first would use more memory. However, I could be wrong, I'm not aware of how this affects processing time.
Here's two C++ examples: This a function which returns a variable:
#include <iostream>
short getInput(){
short input;
std::cin >> input;
return input;
}
int main(){
short input = getInput();
}
You can see in this one that the program needs two input variables to be created, even if the second is only created for a brief amount of time.
This is a function which passes a value by reference:
#include <iostream>
short getInput(short& input){
std::cin >> input;
}
int main(){
short input;
getInput(input);
}
Not only is this example code segment shorter, it only initializes one short variable. Am I wrong on the fact that it's more efficient.