Skip to main content

Multiple Results

Consider the following problem.

Write a function that is given a time duration and determine how many whole minutes/seconds there are in that duration. For example, if it was 135 seconds, this would be 2 minutes and 15 seconds. How would you write a function to obtain this result (not print... but to provide the result back to the calling function). The issue here is that a function has one return value. You can return minutes OR seconds but you can't return both. When we have a situation such as this, where the result is in two components we can use pointers to pass back the results instead of returning the result.

Function Prototype

To solve the above problem, we would write the following function:

This function is passed a duration in seconds and two pointers that point to variables where we want the results to go. The function will calculate the duration in terms of the number of minutes and seconds and pass that result back through numMinutes and numSeconds.

void findDuration(int duration, int* numMinutes, int* numSeconds);

Function call

To use the above function, we would need variables to hold the result and pass the addresses of those variables into the function

void findDuration(int duration, int* numMinutes, int* numSeconds);
int main(void)
{
int minutes;
int seconds;
int dur = 135;
findDuration(dur, &minutes, &seconds);
printf("135 seconds is %02d:%02d\n", minutes,seconds);
return 0;
}

note

This mechanism of passing in addresses to allow alteration to variables by the function is how scanf() works and why we need to pass in the addresses of variables to scanf

The following shows what happens when we make the function call:

Multiple Parameter Passing

Accessing the Variable

The parameters numMinutes and numSeconds hold the address of two variables declared in main. If we were to put the result into numMinutes and numSeconds, we would end up just changing the local variables. What we want to do is put the results back into the variables declared in main().

To do this we use the dereferencing operator. This is denoted as the astericks * character.

Thus, this is how we would write the findDuration function:

void findDuration(int duration, int* numMinutes, int* numSeconds)
{
*numMinutes = duration / 60;
*numSeconds = duration % 60;
}

These two lines of code would do the following:

Multiple Parameter Passing

caution

In the previous struct passing example where we passed the address of a struct to a function we used -> operator to access data members. This is the CORRECT method to do it.

However, it should be noted that the alternative is to dereference the struct using * operator then use the . operator. This method is syntactically correct but it is not preferred as you also need to use brackets to ensure correct order of operations. The . operator is very high in precedence. * is a lot lower so brackets are required to first dereference the pointer then access the data member.

These two statements will do the same thing... but you should use ->

	student->gpa = 3.6;   // <--- This is Better!  use this!
(*student).gpa = 3.6; // <--- don't use this

Pointers allow you to pass back multiple parts of a result from a function.