I'm trying to optimize my code. I've heard that it's better not to use local variables and to reduce the number of function parameters, so currently the structure of my program looks like this:
using namespace std;
const string UPLOAD_LOCATION = "/uploads";
const int value = 100;
const int type = 0;
const int max_value = 255;
string var1, var2, var3;
void func1();
void func2();
void func4();
int main(int argc, char *argv[])
{
func1();
func2();
}
void func1()
{
//do something
}
void func2()
{
func4();
//do something
}
void func4()
{
//do something
}
Would it be more efficient if var1, var2 and var3 were local variables in main function, so I'd pass them as arguments to the functions? I mean something like this:
using namespace std;
const string UPLOAD_LOCATION = "/uploads";
void func1();
void func2();
void func4();
int main(int argc, char *argv[])
{
const int value = 100;
const int type = 0;
const int max_value = 255;
string var1, var2, var3;
var1 = func1(var2, value, type, max_value);
var3 = func2(var2);
}
string func1(string &var2)
{
//do something
}
void func2(string &var2)
{
func4(var2);
//do something
}
void func4(string &var2)
{
//do something
}
If yes, is it better to pass them by reference or by value? For example lets say the function stores the result in string s, then which choice should I make:
a) void func( string in, string &result )
or
b) string func( string in )