# Scope of Names

• 10-13-2009
Brian_Jones
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; }```
• 10-13-2009
tabstop
This has nothing to do with scope of names and everything to do with function calls.
• 10-13-2009
RockyMarrone
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
• 10-14-2009
Brian_Jones
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 :/
• 10-14-2009
tabstop
Quote:

Originally Posted by Brian_Jones
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).
• 10-14-2009
anon
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.
• 10-14-2009
Brian_Jones
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;"
• 10-14-2009
anon
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; }```
• 10-14-2009
Brian_Jones
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-14-2009
Brian_Jones
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.
• 10-14-2009
anon
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; }```
• 10-14-2009
Brian_Jones
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!!