It works. I'm having a problem trying to display the greater number
to the output. For example what ever number is greater the
first or the second it still displays the first number.
Couse that is what is coded in the cout. But I dont know
what to put in it to display the greater number instead
of the fisrt number. Also I dont know how to connect the fisrt part of the code to the second. For example the void block of code is completlly independent of the main. The program will run the same with the void function in or out.

#include <iostream>
using namespace std;


void Max(int& x, int& y)
{                    // start of function body
  int max; 
  int other;       // variable declaration

  if (x >= y) 
  {       // find the maximum number
    max = x;
  }
  else
  {
    (x <= y);
    
    max = y;
  }

}


int main()
{
  void Max( int& , int&);  // function declaration (prototype)
  int firstnum, secnum;

  cout << "\nEnter a number: ";
  cin  >> firstnum;
  cout << "Great! Please enter a second number: ";
  cin  >> secnum;

  cout << "\nThe maximum of these two values is "
       << firstnum << endl; // the function is 
                                             //called here

  system("PAUSE");
  return 0;
}

Your function's prototype should not be inside another function, as your are doing here. It should also be BEFORE the function definition.

Change the function's return type from void to int . The return the value of 'max'. All that's necessary is for you to call the function in the cout call, and the value will be printed out.

By the way, how come you declare the variable 'other' when you don't have any use for it?

you should just output the maximum of the two:

cout << "the max is " << max(firstnum, secondnum)) << endl;

that said, your max function is wrong. It finds the max but should return the result. Here's a cleaner version:

int max(int a, int b)
{
  if(a > b)
    return a;
  else
    return b;
}

All that's necessary is for you to call the function in the cout call, and the value will be printed out.

you should just output the maximum of the two:

cout << "the max is " << max(firstnum, secondnum)) << endl;

That's what I meant to say. Really. :o

I will try to switch that around. About the other I dont know. I'm new to this. It all looks like chinese to me right now. I get the code from the professor I just need to tweek it to his specifications. If you ask me why I do certain things i dont know. thats what i'm trying to figure out.

Thanks for the help guys.

Here's a cleaner version:

int max(int a, int b)
{
  if(a > b)
    return a;
  else
    return b;
}

Except multiple returns from a single function is frowned upon. Best to keep your original function with the return changes mentioned above, and with one other minor change:

if (x >= y) 
{       // find the maximum number
    max = x;
}
else
{
//    (x <= y);    remove this line, it does nothing
    max = y;
}

Except multiple returns from a single function is frowned upon.

Hmm...now that is some news ;)

The statement is not absolute as such. It all depends on the problem under consideration.

Except multiple returns from a single function is frowned upon.

That's more a matter of style for ease of maintenance, AFAICT. IMHO, this function is more clear this way, though if you want a single return, it could be done as

int max(int a, int b) {
  return (a > b ? a : b);
}

but I didn't want to post that one for clarity's sake.

The statement is not absolute as such. It all depends on the problem under consideration.

Which is why I stated it as I did -- not as an absolute. ;)

That's more a matter of style for ease of maintenance, AFAICT. IMHO, this function is more clear this way, though if you want a single return, it could be done as

int max(int a, int b) {
  return (a > b ? a : b);
}

but I didn't want to post that one for clarity's sake.

Very good choice!!

Purists frown on multiple returns. They can be harder to debug because sometimes a return gets lost in complex or long functions. IMO, you should try to use only one return point if the code to support it does not make the code overly complex. But don't make the code difficult to read to accomplish it.

For this piece of code I'd probably use:

int max(int a, int b)
{
    int rtn = a;
    if(a < b)  rtn = b;
    return rtn;
}

I guess it will depend on the coding standards of the company you are working for. Here are two implementations of the function which finds the index of an element in the array and returns -1 if not found.

// MULTIPLE RETURNS

int indexOf2 (int array[], int length, int element)
{
    for (int i = 0; i < length; ++i)
    {
        if (array [i] == element)
            return i ;
    }
    return -1 ;
}

// SINGLE RETURN

int indexOf1 (int array[], int length, int element)
{
    int index = -1 ;

    for (int i = 0; i < length; ++i)
    {
        if (array [i] == element)
        {
            index = i ;
            break ;     // btw even break and continue are frowned upon
        }
    }
    return index ;
}

Take your pick... ;)

Why are these things frowmed upon?

>Why are these things frowmed upon?
Reread WaltP's post:

Purists frown on multiple returns. They can be harder to debug because sometimes a return gets lost in complex or long functions.

Ok got it thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.