Thread: Scope of Names

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    Scope of Names

    I am trying to understand this function and I don't get how at the bottom of the function that "vax(a,b)" can equal just a single integer.
    once "b" equals zero then vax(a,b) somehow equals an integer.

    I am in the learning phase of C++ and this is the first time I have seen that "abcde(int f,int g)" can equal an integer. Looking through my books I can see the form "abcde(int f, int g)" is brought up in something called "scope of names", but I don't understand it.


    Code:
    #include <iostream>
    using namespace std; 
    
    int vax(int a, int b)
    {
    	if(b == 0)
    	{
    	        return a;
    	}
    	else
    	{
    		return vax(b, a % b);
    	}
    }
    
    int main()
    {
         int a,b;
              
         cout << "Input first number: ";
         cin >> a;
         cout << "Input second number: ";
         cin >> b;
         
    	 cout << "Your number is:" << vax(a,b) << endl;
         return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This has nothing to do with scope of names and everything to do with function calls.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Here in function vax regarding scope variable a, and b will be visible and there life time will be in vax function only

    in main() function's a and b will be visible to main function and there lifetime will be restricted to main only

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    ahhh function calls it is. I see it talked about in my books and on webpages, but it doesn't explain how it works. I don't see how "vax(int a ,0)" or abc(int y, int z) can equal an integer :/

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Brian_Jones View Post
    ahhh function calls it is. I see it talked about in my books and on webpages, but it doesn't explain how it works. I don't see how "vax(int a ,0)" or abc(int y, int z) can equal an integer :/

    So read the program and find out how vax(a, 0) can equal an integer. It tells you right there what integer it equals (eventually).

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It doesn't equal an integer. You call a function, and it returns an integer.

    Code:
    int vax(int a, int b) //return type is int
    {
    	if(b == 0)
    	{
    	        return a;
    	}
    	else
    	{
    		return vax(b, a % b);
    	}
    }
    You really need to understand what calling functions and returning values means, before you move on to recursive functions, as in this example.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    It looks like the "a" in the line "return a;" is the same number as the final integer.

    if "a" was 21, "vax(int a, int b)" would be equal to vax(21,0). Somehow with function calls vax(21,0) equals the integer "21".

    I see the "int vax(int a, int b)" is a big part of this.

    But I can't find why "vax(21,0)" displays the number 21 in the last line before "return 0;"

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be you can follow a simpler example (without recursion):

    Code:
    #include <iostream>
    
    int Min(int a, int b)
    {
        if (a < b) {
            return a;
        }
        else {
            return b;
        }
    }
    
    int main()
    {
        std::cout << Min(1, 41) << '\n';
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    Thanks, that helped.

    so, sounds like when
    when function "int Min(int a, int b)"
    is used, all that matters is what is in the first argument position"int a" when it comes to calling it down below.

    Min(1, 41) = 1
    Min(3, 84) = 3

    From what I understand now when calling a function, the integer that is in the first argument position ("f" in "abcde(f,g)") somehow just becomes "f" all by itself once it is called.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    something finally clicked with me about recursive functions.

    when it says "return a", it meant replace the whole "vax(int a, int b)" with just whatever the value "a" is. and that is why on the last line that "vax(a,b)" becomes an integer.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    o_O

    A function returns whatever you ask it to return. Another even simpler example:

    Code:
    #include <iostream>
    
    int Add(int a, int b)
    {
        return a + b;
    }
    
    int main()
    {
        std::cout << Add(2, 3) << '\n';
        return 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    ahhh that last one made everything crystal clear. if there's a cin line, then the ints go into the last line where your Add(2, 3) is, but before they get displayed on that cout line, they go up to the function and then down into the function body, back to rewrite the function-name "int Add(int a, int b)"and then comes back to the last line to overwrite the Add(2, 3). Bing Bang Boom! Got it!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C name space and lexical scope
    By password636 in forum C Programming
    Replies: 1
    Last Post: 09-22-2009, 09:50 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM