Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
This page presents some various ways in which the C programming language passes parameters.
Call by value: pass a copy, original cannot be changed
Call by reference: pass a reference (pointer), original can be changed
2. C preprocessor
Call my macro text replacement, or call by text, is where the preprocessor (part of define) replaces calls with (evaluated) text.
Call by name (not used very much) is like call by text except that calls are not evaluated (and may have some scope code).
3. C routine variations
The following examples are some of the ways in which two integers can be added. In doing so, various methods are used involving variables and arrays.
In the examples, the actual parameters are not changed, but when using call by reference, they could be.
4. Main declarations
The variables x1, y2, and z3are used as local variables in function main.
5. Main assignment
Here variables x1 and y2 are set to values using assignment statements (for example purposes).
6. In-line coding
The value of z3 is to be set to the sum of x1 and y2. There are many ways to do this. One way to do this is in-line coding.
7. Call by value
Call by value passes a copy of the values to the function. Here is the function.
Here is the corresponding call in main.
8. Call by reference
Call by reference passes a reference (pointer) of the values to the function. Here is the function.
Here is the corresponding call in main.
9. Function to/from procedure
It is often necessary to change a function f (as an expression) to or from a procedure p (as a statement). Here is the general example form (for integers).
The function f or procedure p must be written accordingly. Here is the general example form (for integers), written on one line to show the differences.
Here is the C code.
10. Function to procedure
Returning to the original problem, using call by value, the function add2 can be converted to a procedure as follows.
The call needs to be changed appropriately to the following.
By convention (and in most cases) all formal parameters into the function are at the beginning of the list and all formal parameters changed (by reference) are at the end of the parameter list.