call by value and call by reference in cpp code example
Example 1: calling by reference c++
int main() {
int b = 1;
fun(&b);
// now b = 10;
return 0;
}
Example 2: calling by reference c++
void fun(int *a)
{
*a = 10;
}
Example 3: calling by reference c++
void fun3(int a)
{
a = 10;
}
int main()
{
int b = 1;
fun3(b);
// b is still 1 now!
return 0;
}
Example 4: calling by reference c++
void fun2(int& a)
{
a = 5;
}
int main()
{
int b = 10;
fun2(b);
// now b = 5;
return 0;
}