Function parametar

I want to know why I need to mention int x in my loop in order to run program, because I tought that it already remember int x from my previous function with that parametar, and purpose of this program is to print results for unlimited inputs.

#include <iostream>

using namespace std;

void score(int x)
{
if(x < 25 && x > 0)
{
cout << "Your grade is: A" << endl;
}

if(x >= 25 && x < 50)
{
cout << "Your grade is: B" << endl;
}
if(x >= 50 && x < 75)
{
cout << "Your grade is: C" << endl;
}
if(x >= 75 && x <= 100)
{
cout << "Your grade is: D" << endl;
}
if(x < 0 || x > 100)
{
cout << "Error! Type score from 0 to 100" << endl;
}
}

int main()
{
while(true)
{
int x; \\THIS ONE
cout << "Enter you score from 0 to 100 in order to see your grade: ";
cin >> x;
score(x);
}

return 0;
}
It's two different variables that happen to have the same name.

You can pass any int value to score, the name doesn't matter, it doesn't even have to be a variable.

1
2
3
4
5
int x = 1;
score(x); // calls score with the value 1.
int y = 2;
score(y); // calls score with the value 2.
score(3); // calls score with the value 3. 

x inside score holds the value that was passed to the function. It only exists for as long as the function runs and it cannot be accessed from outside the function.
Last edited on
Topic archived. No new replies allowed.