0

I'm a beginner programmer...Here I have a piece of code with two functions...one of them uses a global variable while the other one uses a local static variable...

So here is the code:

//Scope Rules
#include <iostream>
using namespace std;

int num {52};     //Global Variable - Declared outside any class or function

void globalExample();
void staticLocalExample();

void globalExample(){
    cout << "\nGlobal num in globalExample is: " << num << " - start" << endl;
    num *= 4;
    cout << "Global num in globalExample is: " << num << " - end" << endl;
}
void staticLocalExample(){
    static int num {5};      //local to staticLocalExample - retains it's value between calls
    cout << "\nLocal static num in staticLocalExample is: " << num << " - start" << endl;
    num += 20;
    cout << "Local static num in staticLocalExample is: " << num << " - end" << endl;
}

int main() {

    globalExample();
    globalExample();
    staticLocalExample();
    staticLocalExample();

    return 0;
}

The reason I called these functions twice was for me to understand that the second time the function is called, changes will be stored to the variables so the results will be different.

What I don't understand is:

  1. What's the difference between these two functions then?
  2. What does the staticLocalExample function exactly do?(Because in comparison, they both have the same results)
  3. Why and when do we use the keyword static?

Note1:I know the cause of changes is the assignments I have in both functions BUT... This is just an example and I don't quietly understand it

Note2:I checked the link you guys sent me and thank you so much for that...I understood the concepts behind it but still I couldn't find the answer to my third question :)

David Peterson
  • 169
  • 2
  • 9
  • 1
    Related [question](https://stackoverflow.com/questions/12186857/on-local-and-global-static-variables-in-c) if not an outright duplicate. – 1201ProgramAlarm Feb 15 '20 at 20:11
  • 1
    Does this answer your question? [On local and global static variables in C++](https://stackoverflow.com/questions/12186857/on-local-and-global-static-variables-in-c) – walnut Feb 15 '20 at 20:13
  • @walnut I checked the link...I quite understood the concepts behind the problem..but what I couldn't find out was my third question...So thanks for the link :) – David Peterson Feb 15 '20 at 20:25
  • @SamVarshavchik Well to be honest I don't have access to good C++ books in Iran and I also tried to download some of these books but no results...I'm learning through video course...so yeah...that's a big problem – David Peterson Feb 15 '20 at 20:30
  • 1
    The global can be changed anywhere. The static local can only be changed by the function its scope is in. This helps with managing complexity of your code, as it grows bigger (and believe me, codebases can be **huge**) – Not a real meerkat Feb 15 '20 at 20:33
  • 1
    there's not much else to it, really. As everyone else said, this question is better answered by a good book. A book can show and explain to you several reasons for why having a lot of things in the global namespace is a bad idea – Not a real meerkat Feb 15 '20 at 20:35
  • @CássioRenan Thank you...I understood what you said...I think I came to a conclusion and I'll answer my question now – David Peterson Feb 15 '20 at 20:38
  • 1
    C++ is the most complicated and hardest general purpose programming language in use today. The formal technical specifications for C++ run about 2000 pages. Without a good book you will find it to be very difficult to learn much. – Sam Varshavchik Feb 15 '20 at 21:55

1 Answers1

0

I think I came to a conclusion...so please let me know if I'm wrong...

Global Varialble: You can change them and assign different numbers outside or inside of it's scope

Static Variable: You can only change it within it's scope not outside of it

So in conclusion lets modify this code a bit:

#include <iostream>
using namespace std;

int num {52};     //Global Variable - Declared outside any class or function

void globalExample();
void staticLocalExample(int x);

void globalExample(){
    cout << "\nGlobal num in globalExample is: " << num << " - start" << endl;
    num *= 4;
    cout << "Global num in globalExample is: " << num << " - end" << endl;
}
void staticLocalExample(int x){
    static int num {5};      //local to staticLocalExample - retains it's value between calls
    cout << "\nLocal static num in staticLocalExample is: " << num << " - start" << endl;
    num += 20;
    cout << "Local static num in staticLocalExample is: " << num << " - end" << endl;
}

int main() {

    globalExample();
    num = 20;
    globalExample();
    staticLocalExample(10);
    staticLocalExample(15);

    return 0;
}

Output:

Global num in globalExample is: 52 - start
Global num in globalExample is: 208 - end

Global num in globalExample is: 20 - start     <-------
Global num in globalExample is: 80 - end

Local static num in staticLocalExample is: 5 - start
Local static num in staticLocalExample is: 25 - end

Local static num in staticLocalExample is: 25 - start
Local static num in staticLocalExample is: 45 - end

As you can see The global variable changed but the static variable didn't... All I had to do was to think outside the block of code not inside of it...

Thank you Cassio Renan

David Peterson
  • 169
  • 2
  • 9