Programming-Function-Arguments
From DevRandom
Assumptions
1. Syntax is c/c++ like but this is not c or c++
2. Variable ctr has global scope
main()
{
// define 2 element array & assign values
int var[2];
var[0] = 1;
var[1] = 1;
// define a counter variable
global int ctr;
ctr=0;
// print before calling any function
cout << var[0]; // this will print 1
cout << var[1]; // this will print 1
/* Call function with arguments by name
& print */
myfunc_value(var[ctr]);
cout << var[0]; // this will print 1
cout << var[1]; // this will print 1
/* Call function with arguments by reference
& print */
myfunc_ref(var[ctr]); //this results in a call to myfunc_ref(var[0])
cout << var[0]; // this will print 3
cout << var[1]; // this will print 1
/* Call function with arguments by name
& print */
myfunc_name(var[ctr]); // var[ctr] is not evaluated at call time
cout << var[0]; // this will print 1
cout << var[1]; // this will print 3
/* Call function with arguments by value result
& print */
myfunc_value_result(var[ctr], var[ctr]);
cout << var[0]; // this will print 2
cout << var[1]; // this will print 1
/* Function which accepts arguments by value */
FUNCTION myfunc_value(int i, int j) {
ctr = 1;
i = i + 1;
j = j + 1;
// none of these changes will reflect in main
}
/* Function which accepts arguments by reference */
FUNCTION myfunc_ref(int i, int j) {
ctr = 1;
i = i + 1;
j = j + 1;
// when i and j are changed the var[0] is changed
}
FUNCTION myfunc_name(int i, int j) {
ctr = 1;
i = i + 1; // i is expanded as var[ctr] , i.e var[1]
j = j + 1; // j is expanded as var[ctr] , i.e var[1]
// so var[1] becomes 3
}
FUNCTION myfunc_value_result(int i, int j) {
// at start var[0] is copied into i & j. So i = 1, j =1
ctr = 1;
i = i + 1; // i becomes 2
j = j + 1; // j becomes 2
// at the end the values are copied back to main
// var[0] is copied with 2, then var[0] is copied again with 2
}
}






