Understanding Functions, Global Variables, and Local Variables in Python Programming
Python is a general-purpose, high-level, object-oriented programming language. One of the features that makes it distinct from other languages such as C++ and Java, is that it is an interpreted language. This means that python code does not need to be compiled. Because of that it allows for faster software development as compared to compiled languages.
Artificial intelligence witnessed a huge boom over the years when computing power increased exponentially. The next step forward is teaching computers to be automated. In areas of Artificial Intelligence such as Machine Learning, Python is one of the computer programming languages that is leading the charge.
Unlike the real-world Python that would scare any person away, Python is meant to be an easily comprehensible language with easy-to-learn syntax.
Over the years, Python has gained a lot of popularity. It has spanned a community of thousands of coders that is actively developing new projects, tutorials, guides, and helping out fellow coders. And because of that, new comers will find the programming language easier to understand. Your query is only one question away from being answered. Not to mention the various Python online courses available.
Let us explain a few aspects of Python that are also an integral part of the object-oriented programming space.
Functions are blocks of organized code to provide readability as well as reusability. Imagine writing a logic that will follow with a desired output or work towards giving a desired output—that block of code is called a Function.
Each programming language dictates how a function is written, but one common trait among them all is the inclusion of “(“ and “)”. A function is immediately followed by its parameters, also known as arguments. A parameter’s list of a function can consist of more than one or zero parameters. For example, functionname() has no parameters. Similarly, functionname(variablename) has a parameter with a variable “name”.
To construct a function, the following are required:
- Keyword def followed by the name of a function and parenthesis “()”
- Any parameters for a function will have to be written in the parenthesis that follow after the function’s name
- “:” to indicate where the logic of the code begins, also called the body of a function.
To help you understand better, here is a simple Python function:
print ‘Hello’, name
The above function will output the name “Hello Jack”. Let us take a closer look at how the code works.
1. A function named greet with variable “name” is constructed
2. In the code block, the print statement is set to output Hello and whatever value the variable “name” contains
3. We call the function greet and pass the name “Jack” through its parameters
4. Once we have passed the name Jack to the function greet, the output will follow, which is going to be: “Hello Jack”.
Using parameters prove useful in reusability, particularly when there is a need for multiple functions with similar objectives.
Now that you have understood what a function is, it is time to understand local variables. A variable, just like the name suggests, is a container that the user initializes to store value in memory. By referring to variables, we can call the value stored in the memory location of that variable.
A local variable is one that works with limited scope. Meaning that it will only be recognizable and usable within the scope of the function in which it resides. By default, a variable will be local variable, unless specified otherwise. But more on that later.
Let us take an example:
def sum(x , y):
The above code shows a function sum with variables x and y as its parameters. In the second line, we provide the logic in the code block. The output of this code will provide the sum of values stored in variables x and y. In the third line, we return the result to the function.
The fourth line defines the print statement. We call on the function sum and pass values 8 and 6 as the values for x and y, respectively. Again, the logic code of the function sum dictates that the program will add the values stored in variables x and y. Thus, we get the output: 14.
The important thing to note here is that if we were to write another function, sum2 for example, we would not be able to use the variables x and y.
This is how you can use variables of the same name in different functions and it will have no effect on your program.
As we discussed above, local variables have limited scope. They are initialized and serve within the scope of the function in which they are initialized. Another function cannot access variables of a different function. Building on the example we just saw, a different function, greet2 for example, cannot use variable of the function greet. By declaring a variable as global when it is initialized, it can be used outside the scope of its function.
Let us take a closer look with an example:
myvariable = 6
What is happening here is that we defined global a variable named “myvariable” and assigned it value “6” in memory. The next line prints the value stored in variable “myvariable”.
We create a second function named function2() to demonstrate the use of global variables. Notice how we did not have to initialize a variable inside function2(). We simply call upon the variable present in function() and print its value within the scope of function2().
Global variables become useful when you start writing big programs, where the data of one function is tied to the desired output of a second function.
This covers a very short introduction to Python. The concepts presented here are globally applicable to other programming languages such as C++, C#, and Java. If you wish to learn more about Python, there are many Python online courses that will get you coding in no time. And do not forget to engage with the community if you run into problems.