What are the differences between global variables and local variables?

Tip of the week

Created on: February 16, 2018

Updated on: February 18, 2018

Tip of the week

What are the differences between global variables and local variables?

Web development contains a lot of different concepts and paradigms.

Learning them doesn't have to be complicated, they can be explained by breaking down the barriers and that's what we enjoy doing.

When I started learning web development services’ concepts, I used to get confused with the concepts of global and local variables.

Definition of global and local variables

Let's take a look at a definition of global and local variables to start with.

According to the fun programming website:

"Global variables are declared outside any function, and they can be accessed (used) on any function in the program"

"Local variables are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions"

The tiers of government

Let me explain these concepts within everyday language that will enhance your understanding.

In the UK we have two tiers of government

  1. Local Government
  2. National Government

Their roles and responsibilities

Local government’s role or specifically a unitary authority’s role is to provide the following services:

Housing, waste management, waste collection, council tax collection, education, libraries, social services, transport, planning, consumer protection, licensing, cemeteries and crematoria†, police and fire come under shire councils"

National government's role is Central Government's responsibilities include:

Setting and collecting all taxes, except council tax The National Health Service, state benefits such as unemployment benefit, tax credits and state pensions, making and reviewing UK law the Country's security.

The same can be applied to global and local variables, in the fact that local variables have local scope and exist in the function or method that they are in.

Global variables have global scope; this means that they can be used and referenced anywhere within your web development services code.

As in real life you wouldn't be happy if the National government dictated that everyone in a particular areas had to have their bins emptied on a particular day.

I hope that this has explained one of the basic principles of global and local variables.

Let me know in the comments about your experiences with the global and local scope.

Stay tuned for further tips and tricks from www.ormrepo.co.uk/blogs.

The link also provides information about the web development services we offer.