Local Vs. Global Variable

Hello Everyone!

This is my first post on this forum and I'm excited to get involved in this community!

I'm having difficulty understanding why the local variable "int a;" won't store the increment of a++? When I set the variable "int a;" as a global variable then it will store the increment of a++.

Do variables in functions release their values upon completion or return? So if the function gets recalled, it will start out again with the empty variable I initially set?


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
string check_func (int b);

int main()

{
   int option;

   for (int i=0; i<100; i++)
       {
           cin >> option;

           if (option==1)
           {

               cout << check_func(1);
           }
       }
}

string check_func (int b)
{
    int a;

    if(b==1 && a!=1)
        {
            return "hey how is it going";
            a++;
        }
}
So if the function gets recalled, it will start out again with the empty variable I initially set?

Exactly.

-Albatross
So global variables have two purposes?
1. Multiple functions have access to global variables.
2. Static save of value in a variable.

Would there be any other way of doing this other than making "int a;" global?

Also, is it standard to always put global variables above the main function, or would it be fine if I put it above the intended function? As so:

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

int main()
{
   int option;

   for (int i=0; i<100; i++)
       {
           cin >> option;

           if (option==1)
           {

               cout << check_func(1);
           }
       }
}

int a;

string check_func (int b)
{
    if(b==1 && a!=1)
        {
            a++;
            return "hey how is it going";
        }
}
> why the local variable "int a;" won't store the increment of a++?
`a++' in line 27 would never be reached, you break out the function on line 26.
Also, `a' is never initialized


> Would there be any other way of doing this other than (...)
¿of doing what?


> Static save of value in a variable.
¿what's an static save?
Also, is it standard to always put global variables above the main function, or would it be fine if I put it above the intended function? As so:

You can define the global variable pretty much anywhere you want, but remember the global variable will only be visible to functions below the definition.

By the way using global variables should be a method of last resort. Strive to keep your variables as local as possible and learn to properly pass variables to and from your functions.

So global variables have two purposes?
1. Multiple functions have access to global variables.
2. Static save of value in a variable.


For 1. Pass the required variables to and from your functions, avoid globals whenever possible.

For 2. You can also declare the variable static within the function, then it will maintain it's value between function calls. However this variable would still only be visible in the function where it is defined.

I changed the order of a++ in my second reply post on line 24.

I've also replaced "a++;" with "a=1;" in my code.

Would there be any other way of storing a value (which doesn't get released at end of function) in a variable used by a function, without making a global variable?

I mean by saving a value in a variable without having it being released after the function has completed or returned.
Last edited on
Would there be any other way of storing a value (which doesn't get released at end of function) in a variable used by a function, without making a global variable?

Yes -- you can change the storage class by marking it (the variable, at its' declaration) static, thread_local, or extern as needed.

Note that "global" variables are not strictly the same thing as "file-local" variables. The problem is that scope isn't the same thing as linkage, but linkage never seems to get explained in beginner's classes.

The full story can be found here:
http://en.cppreference.com/w/cpp/language/storage_duration
Last edited on
jib:

Would this be a better way of writing my code?

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
string check_func (int a,int b);

int main()
{
   int a=0;
   int option;

   for (int i=0; i<100; i++)
       {
           cin >> option;

           if (option==1)
           {
               cout << check_func(a, 1);
           }
        a=1;
       }
}

string check_func (int a, int b)
{
    if(b==1 && a!=1)
        {
            return "hey how is it going";
        }
}



For 2. You can also declare the variable static within the function, then it will maintain it's value between function calls. However this variable would still only be visible in the function where it is defined.


How do I make a variable static within a function so the value is maintained between function calls?

In other words, is it possible to store a value in a local variable, and not have the value released after the function has ended?
Last edited on
How do I make a variable static within a function
1
2
3
# include <iostream>
int f() { static int x = 0; return x++; } 
int main() { for (int i = 0; i < 10; i ++) std::cout << f() << "\n"; }
closed account (48T7M4Gy)
http://stackoverflow.com/questions/3698043/static-variables-in-c
Topic archived. No new replies allowed.