Section 7f

Functions - Calling and Being Called,

With Differing Parameters


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Calling and Being Called

Simple, but important terminology.

There are just a couple more terms to get familiar with since they are commonly used when discussing the use of functions. When you write the multiplication function operation in the main function, you are asking it to multiply the two numbers and return the result to your variable product. At that point, it is said that you have called the function, and the main function (in this case) is termed the calling function.

The multiplier function that was called upon to conduct its duties is termed the called function. Any function can call any other function in a program with one exception; the main function is not called by any other functions; this is discussed next. However, because you will be working with functions calling other functions (a lot!), you need to become familiar with these terms. The calling function is the one that calls or asks another function to do something. The called function is the function that has been called or asked by another function to do something.

Pass by copy. Another term with which you need to become familiar is related to how data is passed to the called function. When you pass two values to a multiplier function, or one value to a factorial function, the standard action is for your calling function to pass a copy of the value to the function. This process is called, believe it or not, pass by copy. It is not complicated but it is important to know. Since you are passing a copy of your parameter value, the function can change the copy or use or manipulate it as it needs to, and no change will come to the original value passed by the calling function. While this makes pretty good sense, it will become more important later on. The following is a brief example.

.
.
.
// code in program, main function
// assign data
int valOne = 5, valTwo = 10;

swapValues ( valOne, valTwo );

// print the results to the screen
printf( "valOne is: %d, valTwo is: %d\n", result );
.
.
.
// Later in the file, after the main function
void swapValues( int first, int second )
{
int temp = first;

first = second;

second = temp;
}

Resulting output:

valOne = 5, valTwo = 10

Note that the data was not changed in the program even though it was changed in the function.

Pass by reference. An alternative to passing by copy is called passing by reference. This is not commonly implemented but there are times it is necessary. In this circumstance, a value is not passed to the function but instead, the address of that data is passed to the function. This means that the function can manipulate the data in the parameter and the data will be changed in the calling function when the the function has finished. To pass an address to a function, the programmer simply adds an ampersand (&) to the front of the variable. The following is the classic example used by the scanf function. The scanf function conducts the process of capturing data from the user but instead of returning the data as most functions do, it passes the data back as a parameter. Check out the following code.

// prompt user for input
printf( "Enter your height: " );

// acquire the data from the keyboard
scanf( "%d", &heightVal );

The ampersand itelf can be thought of as meaning "The address of:". Again, while this is not frequently done, there are some circumstances where the address of a data item is aquired using the ampersand operator; passing data back from a function by reference is one of these conditions. There will be more to discuss about this in the next chapter when function specifications are created. Another simple example of the use of pass by reference is provided here.

.
.
.
// code in program, main function
// assign data
int valOne = 5, valTwo = 10;

swapValues ( &valOne, &valTwo );

// print the results to the screen
printf( "valOne is: %d, valTwo is: %d\n", result );
.
.
.
// Later in the file, after the main function
void swapValues( int *first, int *second )
{
int temp = *first;

*first = *second;

*second = temp;
}

Resulting output:

valOne = 10, valTwo = 5

In this case the data WAS changed in the program after being changed in the function. However, as can be observed, there is a little more happening in the function here. Look at the different actions:

The concept of passing data by reference, and using pointer actions to accomplish this, will be addressed later in this reference. Stay Tuned.

Watch this video for some further support on functions using the pass by copy tactic. Once again, you should practice the code you see at least two or three times to get fluent with it.