# Variable in the argument_list

Printable View

• 12-16-2008
kenryuakuma
Variable in the argument_list
well...As far as I know, we could use any variable or names in the argument list or parameter providing that the data type and the function name are the same, right? However, this code won't work if I replace factor_num with n in the argument_list of the function definition?

Code:

```#include <iostream> int triangle(int); int main() {     using namespace std;     // Declare a variable as fact_num that represent factorial number     int fact_num;     // Promt user for input     cout << "Please enter a number: ";     cin >> fact_num;     cout << "Your factorial number is: " << triangle(fact_num) << endl;     system("pause");     return 0; }     // The function body. That's the function definition     int triangle(int n){         int n;         int sum = 0;         for(n = 1; n <= fact_num; n++)             sum += n;             return sum; }```
This program doesn't work.

But this one with different names for the argument_list in the main() or in the function definition works really well. I am just wondering why. That really baffles me.

Code:

```#include <iostream> #include <cmath> using namespace std; int prime(int n); int main(){     int i;     while(1){         cout << "Enter a number (0 to exit)";         cout << "and press ENTER:";         cin >> i;         if(i == 0)             break;         if(prime(i))  // This is what I am talking about, see                       // the (i) variable in the argument_list             cout << i << " is prime" << endl;         else             cout << i << " is not prime" << endl;     }     return 0; } int prime(int n){      // See this. the varialbe in the argument_list     int i;            // is now n, not i.     for(i = 2; i <= sqrt(static_cast<double>(n)); i++){         if(n % i == 0)             return false;     }     return true; }```
Besides, how come the author has to set [b]while (1) {()} before the function call?
• 12-16-2008
C_ntua
The two version are not the same!
First of all you cannot use fact_num inside triangle. It is declared in main so it can only be used in main. You can use variables that have the same or "bigger scoping". So if a variable is global all functions can use it. If not then it is local and used only for that variable.
The error you make is that you think that because triangle is called from main it can use its varariables. Wrong. Every function is on the same scope. Think that triangle can be used everywhere. Even in another function except main.

The second error is that you declare twice n. One as a parameter and one as a local variable. You don't do that. You have to give different names, since they are different things.

Post if you want to elaborate
• 12-16-2008
kenryuakuma
I understand what you meant by the second error...But I am just a bit confused about the first reason why you cannot use "fact_num". I do know global and local variables. Would you please list some examples with a bit of explanation?
• 12-16-2008
tabstop
If you knew global and local variables, you wouldn't be asking the question. The variables you declare in main, specifically in your case fact_num, are local to that function, just like the variables you declare in prime, specifically i and n (declared in the parameter list), are local to that function.
• 12-17-2008
C_ntua
Here is an example:
Code:

```int g; int fun1(int a) {     int a;  } int fun2() {     int a;     g = 5; //correct     b = 3; //wrong! } int main() {     int a;     int b }```
So:
main: can use a, b, g
fun1: can use a, g
fun2: can use a, g
Every local variable has like a "full name". Its scope and its name. So more precicely:
main: can use a_main, b_main, g
fun1: can use a_fun1, g
fun2: can use a_fun2, g