Parameters of functions:
|
Constants can
only be
passed (= stored)
by value
I.e.:
you can only copy
the
value of the
constant to
the parameter store location
Variables can by
passed in
different ways to
a function !!!
The 2 common ways are:
(1) by-value
and
(2) by-reference
The caller function and the callee function will coorperate as follows:
|
All high level programming languages provides/implements the pass-by-value mechanism
Pass-by-value illustrated using C++:
#include <iostream> using namespace std; void f(int x) // Var x is passed by value { x = x + 1; } int main(int argc, char *argv[]) { int k = 7; cout << "before f( ): " << k << endl; // Prints: 7 f(k); cout << "after f( ): " << k << endl; // Prints: 7 } |
DEMO: /home/cs255001/demo/tmp/demo.C
The caller function and the callee function will coorperate as follows:
|
C++ and Pascal are
programming languages that
provides/implements
pass-by-reference
Java
does not !!!
Pass-by-refernce illustrated using C++:
#include <iostream>
using namespace std;
void f(int & x) // Var x is passed by reference
{
x = x + 1;
}
int main(int argc, char *argv[])
{
int k = 7;
cout << "before f( ): " << k << endl; // Prints: 7
f(k);
cout << "after f( ): " << k << endl; // Prints: 8
}
|
DEMO: /home/cs255001/demo/tmp/demo.C
|
These are the defined behavior of pass-by-value and passed-by-reference that is implemented by the compiler !!