Functions3 - An example to illustrate the difference...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
An example to illustrate the difference between actual and formal parameters In this program, the user will enter four values. The program will print out the sum of the pair of largest values. A function that may help us in this program is one that determines the maximum of three integers. Here is such a function: int Max3(int a, int b, int c) { if ( return a; else if (b > c) return b; else return c; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Now, we can use that function as follows in our program: #include <stdio.h> int Max3(int a, int b, int c); int main() { int a, b, c, d; int max1, max2; printf(" Enter the four values.\n"); scanf ( "%d%d%d%d", &a, &b, &c, &d); // Read input . max1 = Max3 (a+b, a+c, a+d); // Compute max of three pairs max2 = Max3 (b+c, b+d, c+d); // Compute the max of the // other three pairs. if (max1 > max 2) printf ("The sum of the two largest values is %d\n", max1); else printf ("The sum of the two largest values is %d\n", max2); return 0; } // Pre-condition: All parameters are integers. // Post-condition: The largest value of the three is returned. int Max3(int a, int b, int c) { if ( return a; else if (b > c) return b; else return c; }
Background image of page 2
In this previous example, in the function definition, there were three variables, a, b, and c. These are the formal parameters for the function. In main, we called the Max3 function twice. On the first call, we passed the following actual parameter: a+b, a+c, and a+d. Note that the a, b, and c represented here are DIFFERENT than the a, b, and c that are formal parameters in the Max3 function. In general, each function gets to have its own variables. But within a function you can NOT have two variables with the same name. BUT, you CAN have two variables with the same name in two different functions. Thus, when you trace through code, it makes sense to refer to variables with subscripts, like so: a main or a Max3 . Furthermore, it's also important to realize that the Max3 function can be called multiple times, so that for EACH separate function call, the Max3 function has a copy of each of its own variables. (There are two types of variables a function
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/09/2011 for the course COP 3223 taught by Professor Guha during the Spring '08 term at University of Central Florida.

Page1 / 10

Functions3 - An example to illustrate the difference...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online