Variable in the argument_list

This is a discussion on Variable in the argument_list within the C++ Programming forums, part of the General Programming Boards category; well...As far as I know, we could use any variable or names in the argument list or parameter providing that ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    65

    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?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    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?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  2. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  3. Replies: 2
    Last Post: 04-12-2004, 02:37 AM
  4. write Variable and open Variable and get Information
    By cyberbjorn in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 02:30 AM
  5. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21