+ 3
what is the difference between passing argument by reference and passing function
3 ответов
+ 4
Pass by value is the regular way you would pass a variable through a function:
void seta(int a)
{
a = 10;
}
int main()
{
int a = 5;
seta(a);
cout << a;
//output is 5, because the function created a copy of variable a from main
}
Pass by reference is when you pass through a variable when the function parameters are that of a memory address using the ampersand (&):
void seta(&a)
{
a = 10;
}
int main()
{
int a = 5;
seta(a);
cout << a;
//output is 10, as we passed by reference, allowing the function to modify the variables memory address, rather than creating a copy and modifying that
}
+ 1
@cohen creber nice example
@aditya good question
0
At compile time memory gets allocated and deallocated is called dynamic memory.