=============================== DEFINITIONS ============================== Value: In computer science, a value is a sequence of bits that is interpreted according to some data type. Reference: In computer science, a reference is an object containing information which refers to data stored elsewhere, as opposed to containing the data itself. Accessing the value referred to by a reference is called dereferencing it. References are fundamental to constructing many data structures (such as linked lists) and in exchanging information between different parts of a program. Pointer: In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. Obtaining the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++). ==================================================================== * Call-by-value. Pass the r-value of A[j]. * Call-by-reference. Pass the l-value of A[j]. * Call-by-name. Pass the text of A[j] itself, avoiding "name clashes." ================================= C ================================= pass by value semantics --------------------------------------------------------------------------- int a = 5; int *money = NULL; money = &a; *money = 8; malloc(int variable) free(pointer) --------------------------------------------------------------------------- --------------------------------------------------------------------------- void alter(int *n) { *n = 120; } void func(void) { int x = 24; alter(&x); } --------------------------------------------------------------------------- --------------------------------------------------------------------------- int *ptr = malloc(sizeof (int)); free(ptr); *ptr = 0; --------------------------------------------------------------------------- --------------------------------------------------------------------------- int array[5]; int *ptr = array; ptr[0] = 1; *(array + 1) = 2; --------------------------------------------------------------------------- --------------------------------------------------------------------------- ================================= C++ ================================= --------------------------------------------------------------------------- --------------------------------------------------------------------------- int& preinc(int& x) { ++x; return x; } preinc(y) = 5; --------------------------------------------------------------------------- --------------------------------------------------------------------------- void swap ( int lhs, int rhs) { int temp = lhs; int lhs = rhs; int rhs = lhs; } void swap ( int& lhs, int& rhs);} int temp = lhs; int lhs = rhs; int rhs = lhs; } --------------------------------------------------------------------------- ================================= Java =================================== --------------------------------------------------------------------------- public void swap(int var1, int var2) { int temp = var1; var1 = var2; var2 = temp; } --------------------------------------------------------------------------- public void swap(Integer var1, Integer var2) { Integer temp = var1; var1 = var2; var2 = temp; } --------------------------------------------------------------------------- public void tricky(Point arg1, Point arg2) { arg1.x = 100; arg1.y = 100; Point temp = arg1; arg1 = arg2; arg2 = temp; } public static void main(String [] args) { Point pnt1 = new Point(0,0); Point pnt2 = new Point(0,0); System.out.println("X: " + pnt1.x + " Y: " +pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); System.out.println(" "); tricky(pnt1,pnt2); System.out.println("X: " + pnt1.x + " Y:" + pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); } --------------------------------------------------------------------------- ============================= PYTHON ==================================== --------------------------------------------------------------------------- def foobar(a): a = 0 a = 1 foobar(a) a = [1, 2, 3] foobar(a) >def foobar(a): > a *= 2 a = 1 foobar(a) a = [1, 2, 3] foobar(a)