mardi 29 octobre 2019

Performing function on variable by referance vs setting variable equal to function outcome

Below is some general code that accomplishes the same task and requires relatively the same amount of typing. It is my understand that the first method initializes the variable directly to desired output while the second first creates a variable initialized to null and later changes it. Does this mean the first method runs faster? Additionally is there any reason why I would use one method over another?

int main()
{int x=foo(2);}

int foo(x)
{return x*x;}

vs

int main()
{int x;
foo(x);}

void foo(&x)
{x*x}

Aucun commentaire:

Enregistrer un commentaire