Polymorphic Data Structures in C/Function Invocation

All C programs contain function calls. For example, in the simplest "Hello World" program, one function call is used: The function printf takes in the string "Hello, world.\n" as a parameter, and produces the text on the screen as a result.

Simple Parameters
When a function is called, memory space is allocated for that function's local variables and the values of the parameters are copied and placed into the function's memory space. This is why C is sometimes referred to as a "call by value" language. A simple way to examine this principle is by using a program called No Swap. The output of this program would be: The intent of the program is to swap the values of x and y, but clearly this is not the case. This is because the actual variables x and y are not passed to the function noswap, but rather copies of the variables are made and pushed onto the function's runtime stack. It is possible to manipulate this behavior, along with the return value of a given function, to create the illusion of instantaneous processing. The code below is a simple addition program that demonstrates the ability to add two integers together. Try to edit the add function to cause it to subtract, multiply, or take the power of the numbers (the most difficult without adding additional C libraries) instead of adding them.

All types in C can be passed by value, including structures, unions, and arrays.

Pointers as Parameters
Using pointers as parameters is the only way to modify local variables in functions other than the one that declared it. Using pointers as parameters is a method known as "simulated call by reference". Its usage is fairly straightforward: instead of passing a copy of the value of a variable, the program simply passes a pointer to the actual value in the calling function. Because of this, the variable must be dereferenced in the called function. Let's examine No Swap again, except it will now be known as Swap. The output of this program would be: Notice that when swap is called, the address-of operator (&) is used. This is because neither x nor y are pointers. Appending a variable name with the address-of operator causes a pointer of the variable to be passed instead of a copy. Since pointers are simply references to the address of a variable, a copy of the pointer is made and passed to the function, which is why this method is only simulated call by reference and not true call by reference.

Arrays as Parameters
Arrays are treated differently than other variables. This is because their name (without a subscript [] to denote a specific value) is simply a pointer to the first item in the array. Therefore, A and &A[0] are the same. Additionally, dereferencing as *A would return the value of A[0]. Because of this behavior, it is extremely easy to pass arrays into a function and modify them. Notice how the output is the same on both lines, even though the address-of operator was not used when <tt>array_copy</tt> was called. This is because array bases are already pointers.