scope of the identifiers

In the textbook that I am using to learn C++, it addresses scope in a rather vague fashion. There is an example of a simple code that illustrates how the scope of identifiers works. I have typed that out below. It makes sense for the most part, but I have trouble understanding how the scope/visibility of functions work in relation to other functions.

Next to the coded example in the textbook is a table. The table summarizes the scope/visibility of the identifiers in the code. I have tried to provide a recreation (very crude) of select identifiers from the table.

the specific question that I have is how come void function (one) is not visible to all other functions the way void functions (two and three) are? My first impression is that it has something to do with the parameters of function one.


visibility = vis
Identifier
one (func name) (vis in 1)Y (vis in 2)Y (vis in 3)N (vis block4)N (vis in main)Y

two (func name) (vis in 1)Y (vis in 2)Y (vis in 3)Y (vis in block4)Y (vis in main)Y
three (func name) (vis in 1)Y (vis in 2)Y (vis in 3)Y (vis in block4)Y (vis in main)Y

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include
using namespace std;
const double RATE = 10.50;
int z;
double t;

void one(int x, char y);
void two(int a, int b, char x);
void three(int one, double y, int z);

int main()
{
     int num, first;
     double x, y, z;
     char name, last;
     .
     .
     .
     return 0;
}

void one(int x, char y)
{
     .
     .
     .
}

int w;

void two(int a, int b, char x)
{
     int count;
     .
     .
     .
}

void three(int one, double y, int z)
{
     char ch;
     int a;
     .
     .
     .
     //Block four
     {
          int x;
          char a;
          .
          .
     } // end Block four
       .
       .
       .
}


thanks,
Ddrake44
You have a case of name hiding.
void three(int one, double y, int z)
That one variable hides one function

Like a hiding in lines 42 and 49
Last edited on
As a function is called it only knows variables either declared within, declared as global (available in the file past the point of declaration) or sent in as parameters. It does not recognise anything that does not meet one of these requirements. (This is because a new stack is created to handle the variables and calculations. But this is a more advanced topic you will learn about when you study Recursion).

Note: the be specific when a variable is passed into a function it is essentially passed as a value, thus a new name will be issued to the value and that will be what the function recognises. It will not recognise the name of the value before passed in.

To answer your specific question.


the specific question that I have is how come void function (one) is not visible to all other functions the way void functions (two and three) are? My first impression is that it has something to do with the parameters of function one.


function one() should be visible to the other functions as they are declared in this block

1
2
3
4
5
6
7
8
9
#include
using namespace std;
const double RATE = 10.50;
int z;
double t;

void one(int x, char y);
void two(int a, int b, char x);
void three(int one, double y, int z);


If they were not declared then the functions themselves would only be visible to the proceeding functions. I believe the explanation is referring to the variables and not the functions themselves.

If you accept the answers provided please mark thread as solved.
Last edited on
Topic archived. No new replies allowed.