# more trouble with functions

• 03-23-2003
volk
more trouble with functions
Code:

```#include <iostream> using namespace std; int numbers(int, int, int, int); int findHighest(int, int, int, int); int main() {                        int num1 = 0;         int num2 = 0;         int num3 = 0;         int num4 = 0;                 int highest;         numbers(num1, num2, num3, num4);         highest = findHighest(num1, num2, num3, num4);         if (highest == num1)         cout << "The highest number entered was the first one" << endl;         if (highest == num2)         cout << "The highest number entered was the second one" << endl;         if (highest == num3)         cout << "The highest number entered was the third one" << endl;                 if (highest == num4)         cout << "The highest number entered was the fourth one" << endl;         return 0; } int numbers(int num1, int num2,                           int num3, int num4) {         cout << "Enter four numbers" << endl;         cin >> num1;         cin >> num2;         cin >> num3;         cin >> num4;         return 0; } int findHighest(int num1, int num2,                                 int num3, int num4) {         int highest;         if (num1 > num2 && num1 > num3 && num1 > num4)                 highest = num1;                         else if (num2 > num1 && num2 > num3 && num2 > num4)                 highest = num2;                         else if (num3 > num1 && num3 > num2 && num3 > num4)                 highest = num3;                         else                 highest = num4;                         return highest; }```
Why do all of the cout statements execute? highest can't equal num1, num2, num3, and num4 at the same time if they're all different.
• 03-23-2003
Hammer
>>highest can't equal num1, num2, num3, and num4 at the same time if they're all different
But they're not different. Do some debugging on your own to solve this one, it'll be good practice for you. Start by printing out the values of num1-4 in main.
• 03-23-2003
CheesyMoo
Do you know how to use refrence variables? You might want to look into that.
• 03-24-2003
limacat
Re: more trouble with functions
Quote:

Originally posted by volk
Code:

```#include <iostream> using namespace std; int numbers(int, int, int, int); int findHighest(int, int, int, int); int main() {        <...>         numbers(num1, num2, num3, num4); <...>```

Actually this code doesn't change the numX variables. You have to use the pass by reference mechanism...

Code:

```void numbers(&amp;int, &amp;int, &amp;int, &amp;int); int numbers(int num1, int num2,                           int num3, int num4) {         cout << "Enter four numbers" << endl;         cin >> num1;         cin >> num2;         cin >> num3;         cin >> num4;         return 0; }```
A good thing to do would be also to spend some time to write down the name of the parameter you are passing. Now it may not seem to see clear why, but when you will be working with .h include files, you will have to know what a function does by only watching its definition, so that if you find something like that:

int insuranceCost(car insuredCar, person client);

You will be able to tell what the function does.

Something like:
int foo(int int int int)

is not clear.

Quote:

Why do all of the cout statements execute? highest can't equal num1, num2, num3, and num4 at the same time if they're all different.
Because you never change the original ints, which are still set to 0. So findhigerst returns 0, and the ifs are all executed.

When you will be more expert, I advice you to:

- to use a variable list of integers and an integer representing the lenght of the list
- use a linear scanning algorithm to find the maximum

That is (in pseudocode)

Code:

```int max = the first element of the list; int maxPosition = the first element of the list; listpointer = go to the next place of the list; add 1 to the list count; while (the list hasn't finished) {   if (max < the current element of the list) {     max = the current element of the list;     maxPosition = the current elementPosition of the list;   }   listpointer = go to the next place of the   list;   add 1 to the list count; }```