Chapter 6.1 Functions Procedures with Simple Output
Parameters
A function may have only a single return value. Sometimes we want
a subprogram to return multiple values. In this case, we need to
return values through the parameters. We call these "output
parameters." In math and computer science, functions do not have
output parameters. The correct computer science term for a subprogram
that has output parameters is "procedure."
How does a procedure produce output parameters?
/* This program compiles and executes without errors */
#include <stdio.h>
int badidea; /* Never declare global variables */
/* Function with a return value to square a number */
int square(int num)
{
return num * num;
}
/* Procedure to square and cube a number - The Obvious Approach */
void square_cube(int num, int square, int cube)
{
square = num * num;
cube = square * num;
printf("%d %d %d\n", num,
square, cube);
}
int main()
{
int able = 5;
int answer = 0;
int result1 = 0;
int result2 = 0;
answer = square(able);
square_cube(able, result1, result2);
printf(" %d \t %d \t %d\n", answer, result1, result2);
return 0;
}