# Modular Programming Help

• 03-19-2013
trintiin
Modular Programming Help
Hi, I'm looking for help with a homework question. Any assistance will be greatly appreciated. Essentially, I am supposed to write a function (max_of_3), and call it in the main function. I'm getting a logical error, so please tell me if you notice any redundancies or wrong steps I have taken. (Don't mind the part comments.. There were individual parts for my assignment.) The function is supposed to take in three numbers, and find and return the largest of the three. If you look under the issue comment, you will see where I have attempted to call the function to display it.

Code:

```#include <stdio.h> int max_of_3(int x, int y, int z) {     int l;         if (x > y && x > z)         l = x;     else         if (y > x && y > z)         l = y;     else         if (z > x && z > y)         l = z;                return (l); } int main(void) {                                       /*Part 1 & 2*/     int a, b, c, larger, largest, largest1, smaller, smallest, x, y, z;     printf ("Enter a number: ");     scanf ("%d %d %d", &a, &b, &c);     printf ("\nThe first number entered was %d.", a);     printf ("\nThe second number entered was %d.", b);     printf ("\nThe third number entered was %d.", c);                                     /*Part 3 & 4*/         if (a > b)         larger = a;     else         larger = b;         printf ("\n\n%d is the larger number of the first two.", larger);        if (larger > c)         largest = larger;     else         largest = c;         printf ("\n\n%d is the largest number.", largest);                                                                                                                                        /*Part 6*/                                                                                  if (a > b)         smaller = b;     else         smaller = a;         printf ("\n\n%d is the smaller number of the first two.", smaller);         if (smaller > c)         smallest = c;     else         smallest = smaller;         printf ("\n\n%d is the smallest number.", smallest);                                               /*Part 7*/                                                                                                                        printf ("\n\n%d is the middle number.", a+b+c-smallest-largest);                                               /*Part 8*/                                                                                                                                                     printf ("\n\nFrom least to greatest, the numbers are: %d, %d, %d.", smallest, a+b+c-smallest-largest, largest);                                                  /*issue*/     largest1 = max_of_3(x, y, z);     printf ("\n\n\n\n %d", largest1);         return (0); }```

Again, any help is very much appreciated. Thanks.
• 03-19-2013
Matticus
Your understanding of functions is slightly off.

Code:

`int max_of_3(int x, int y, int z)`
That means within this function, there are three integer variables (x, y, and z).

When you pass arguments to the function, it doesn't matter what they're called. Your function isn't looking for variables named 'x', 'y', and 'z'. It's simply looking for three integer values.

So in the context of your program, this is not correct:

Code:

`largest1 = max_of_3(x, y, z);`
...because 'x', 'y', and 'z' aren't used (though they're declared). In "main()" (where you call this function), you're using 'a', 'b', and 'c'. So those are the variables you want to pass to the function.
• 03-19-2013
Matticus
One more point: 'l' (lower case L) is a poor variable name, as in some character sets, it can be confused with the number one or a capital 'i'. You can, and should, make your variable names more descriptive.
• 03-19-2013
trintiin
Thanks a lot.. It works. Your explanation was very clear; I understood the moment I read it. I wish I could do more, but your help will contributes to a good mark, and a happy student. :) I will also keep your second reply in memory. Thanks a lot, God bless you sir.