Function Parameters

Why can't I write int getWhatTheyWant(int choice) instead? Isn't those () used to declare variables that are about to be used inside the private function getWhatTheyWant()?

1
2
3
4
5
6
7
8
9
10
11
int getWhatTheyWant(){
    int choice;

    cout << "1 - Plain Items" << endl;
    cout << "2 - Helpful Items" << endl;
    cout << "3 - Harmful Items" << endl;
    cout << "4 - Quit Program" << endl;

    cin >> choice;
    return choice;
}



Aren't these two the same?


1
2
3
4
5
6
7
8
9
10
int getWhatTheyWant(int choice){
   
    cout << "1 - Plain Items" << endl;
    cout << "2 - Helpful Items" << endl;
    cout << "3 - Harmful Items" << endl;
    cout << "4 - Quit Program" << endl;

    cin >> choice;
    return choice;
}
Last edited on
You should be able to, but there isn't anypoint in having an argument if it only going to be written over.
In your 2nd example the variable is being passed to the function externally. The value is therefore already known before the function call occurs (hopefully.) In the 1st you are getting the value from the user. Just look at how you would call each function:

1
2
3
 getWhatTheyWant() // 1st fuction

getWhatTheyWant(alreadyEnteredValue) //2nd function 


This is assuming you had some mechanism for already assigning a value to alreadyEnteredValue before you called it, of course.
Last edited on
Topic archived. No new replies allowed.