What are global variables?

A global variable is like a toy that all your friends can play with at once, no one has to pass it around.

Imagine you and your friends are playing in a big room. You each have your own toys, but there's also a special ball that everyone can use. That ball is like a global variable, it’s not just for one person; it belongs to the whole group.

How It Works

When you're playing, you might kick the ball to someone else. But because it's a global variable, anyone can touch it at any time. If your friend adds a sticker to the ball, you'll see that sticker when you look at it next, no need for them to tell you!

Why It Matters

Without a global variable, each person would have their own separate toy. That’s fine too, but sometimes sharing makes things easier. Like if everyone is playing the same game and needs the same ball, having one shared global variable keeps everything connected and simple.

So next time you're playing with friends, remember: the special ball they all use is just like a global variable in a program!

Take the quiz →

Examples

  1. A global variable is like a shared notebook that all students in a class can use to write down important information.
  2. Imagine a teacher writes the homework on the board, every student can see it and use it for their work.
  3. If one student adds more notes, everyone else sees them too.

Ask a question

See also

Discussion

Recent activity

Categories: Science · variables· programming· scope