Is my function defition right?

Apr 24, 2011 at 6:05pm
Hello,
I needed to create a function definition so that in returns the greatest of the two input parameters.

This is what I have. I think it is right. The first two paramenter receive data from the user than the last one returns an parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Max (int num1, int num2, int& greatest);
{ cout << “Enter in two different integers:” << endl;
cin >> num1 >> num2;

if  
num1 > num2;
num1==greatest;
cout << num1;


else if
num2 > num1;
num2==greatest;
cout<<num2;

}

Apr 24, 2011 at 6:10pm
No.
Try this:
1
2
3
4
5
6
7
8
9
10
11
int Max(){ //no semicolon in the definition
int num1, num2; //these don't need to be parameters

cout << "Enter 2 numbers: ";
cin >> num1 >> num2;

if (num1>num2)
return num1;

else
return num2;}


The function here gets 2 numbers, then returns the larger.
Apr 24, 2011 at 6:27pm
Just want to make sure I understand.

I don't have to include greatest, but it would be still output?
Apr 24, 2011 at 6:48pm
What do you mean by "output"?

Do you understand the idea of returned values from functions?
Apr 24, 2011 at 6:49pm
correct. You can treat max like an int pretty much.
Like this:
1
2
3
4
5
6
7
int main(){
    int greatest = Max(); //it will get 2 numbers and return the larger, then greatest is set to the 
                                     //return value of Max
    /*
    more code...
    */
    return 0;}
Last edited on Apr 24, 2011 at 6:50pm
Apr 24, 2011 at 9:00pm
Ok. Thanks for the help very much.
Topic archived. No new replies allowed.