In the"Pointer fun with Binky" video, Nick Parlante introduced the terminology of pointee for the "thing a pointer points to". In the same way, it may be helpful to introduce two similar words when talking about a specific function call:
In example1.c
at the left below, there are exactly two function calls that appear in the code as we read from top to bottom. In this case, that also happens to be the order in which the function calls would occur in time.
main
is the caller, and squared
is the callee.main
is the caller, and printf
is the callee (keep in mind that printf
is a function call).In example2.c
at the right below, there are five function calls—I list them here in the order in which they can be found in the code reading from top to bottom. However, this is NOT the order in which they would actually occur in time. In fact, on any given run of the program, depending on whether argc==2
or argc!=2
, which functions get called is different.
doStuff
is the caller, and twiceTheValue
is the callee.doStuff
is the caller, and printf
is the callee.main
is the caller, and printf
is the callee (This only happens if argc!=2.)
atoi
is nested inside the actual parameter of the call to doStuff
atoi
, main
is the caller, and atoi
is the calleedoStuff
, main
is the caller, and doStuff
is the calleeThis should come as no surprise to you at this stage in your study of programming but it is worth mentioning: in general, the order of functions that gets called, and even which functions get called (or not) may depend on the input to the program, and may be different from the order in which the function calls appears when reading top to bottom in the code. Can you work out which functions get called, and in what order, whem argc==2
, and the case where argc!=2
?
// example1.c from H23, CS16, S10 #include |
// example2.c from H23, CS16, S10 #include |
Please turn over for more...
...continued from other side
When passing parameters to a function, there are two ways it can happen—well, three if you count passing an array as a separate way:
int myFunc(int x)
;—the value is copied from an expression in the caller to an expression in the calleeint myFunc(int *p);
a pointer is passed from the caller to the callee. int myFunc(int nums[], int n);
which is really just a special case of passing a pointer—essentially another way of writing
int myFunc(int *nums, int n);
We've been over these in lecture, and they are covered in your Etter textbook—so the information above is really just review.
In C++, there is yet one more way to pass parameters, and the syntax can be a bit confusing—it adds yet another meaning for the overworked ampersand. So before we go there, let's review the meaning that the ampersand already has:
scanf("%i",&x);
)
printf("%i\n",x & y);
)
if (x!=0 && y!=0)
In C++, there is yet one more way to pass parameters. It is very similar to the way passing by pointer works in C, but the syntax is simplified.
In C, to pass by pointer, in the callee, we write something like what you see below in squareIt.c at the left, if, for example, the effect we want is that the variable passed in gets its value squared. Note the following:
In C++, we can still write exactly the same code—the example on the left is valid C++. However, there is another option, as shown on the right in the file squareIt.cpp
(as you may recall, .cpp
is one of the valid filename endings for C++ code.)
The effect of the C++ code is exactly the same as that of the C code, but the syntax is simplified.
Look at the syntax in the C++ example for the function header: void squareIt(int &x)
and note that the & here does NOT MEAN ADDRESS OF. This is different from the address of syntax because the & occurs in front of a variable that is a formal parameter—when it occurs here, in the header of a function definition or a function prototype, it means that the parameter is a reference parameter. A reference parameter can be thought of "similar passing by pointer, but with automatic transmission as opposed to standard transmission". That is:
A reference parameter creates an alias—in this example, inside the function squareIt, x is an alias for a in the main program. (We could also say that in the C example, (*p) is an alias for a.)
// squareIt.c #include |
// squareIt.cpp (using C style I/O) #include |