Python is a convenient language that’s often used for scripting, data science, and web development.
In this article, we’ll look at how to define and use Python functions.
Functions
A function is a piece of code that we can invoke repeatedly in different places.
It’s defined with the def
keyword as follows:
def greet(name):
print('Hello', name)
The greet
function above has one parameter. We can pass in an argument to set the value of the parameter when we call it.
Functions always start with the def
keyword, then the function name, then parentheses and zero or more parameters inside. Then the first line ends with a colon.
The code for the function is indented inside the function block.
For example, we can call greet
as follows:
greet('Joe')
Then we see:
Hello Joe
displayed on the screen since we passed in 'Joe'
as the argument of the greet
function call.
Functions can call other functions. For example, we can write the following code to make our greet
function call another function:
def greet(first_name, last_name):
print('Hello', full_name(first_name, last_name))
def full_name(first_name, last_name):
return '%s %s' % (first_name, last_name)
greet('Jane', 'Smith')
In the code above, our greet
function calls the full_name
function which returns the full name constructed by combining first_name
and last_name
.
In the full_name
function, we use the return
keyword to return the computed result of combining the first_name
and last_name
parameters into one string.
The function ends whenever we use the return
keyword. Nothing below it will run.
Therefore, we can use it as follows to return the values that we want to return conditionally by using multiple return
statements as follows:
import random
def crystal_ball(num):
if num == 1:
return 'It is a great day'
elif num == 2:
return 'It is a lucky day'
elif num == 3:
return 'It is an auspicious day'
r = random.randint(1, 4)
fortune = crystal_ball(r)
print(fortune)
In the code above, we have if
statements to return something whenever the value of num
is 1, 2, or 3.
If num
is 1, then crystal_ball
returns 'It is a great day'
.
If num
is 2, then crystal_ball
returns 'It is a lucky day'
.
If num
is 3, then crystal_ball
returns ‘It is an auspicious day’
.
Once something is returned, crystal_ball
function stops running.
We can then assigned the returned value to another variable for storage as we did by writing:
fortune = crystal_ball(r)
Then we printed out the value of fortune
.
The None Value
In Python, we have the value None
to represent no value. None
has the type NoneType
.
None
has the capital N
.
We can use it as a return value of something that shouldn’t have a value.
For example, the print
returns None
since there’s nothing to return. It just prints a value on the screen.
If we write:
foo = print('Hello!')
print(None == foo)
In the code above, we should see:
Hello!
True
printed on the screen since print
returns None
, so the value that’s assigned to foo
would be None
.
Therefore, None == foo
returns True
.
Keyword Arguments
Python functions can have named arguments. This way, we know what values the arguments are set when we’re passing them.
For example, we can pass in named parameters as follows:
def full_name(first_name, last_name):
return '%s %s' % (first_name, last_name)
print(full_name(first_name='Jane', last_name='Smith'))
In the code, we called full_name
by writing:
full_name(first_name='Jane', la_name='Smith')
Now we know that we’re passing 'Jane'
as the value of the first_name
parameter and 'Smith'
as the value of the last_name
parameter.
The Call Stack
The call stack is a data structure that tells us what function we’ve called in the order they’re called.
The earliest called function is at the bottom of the stack, and the later ones are higher in the stack.
For example, in the example we have earlier:
def greet(first_name, last_name):
print('Hello', full_name(first_name, last_name))
def full_name(first_name, last_name):
return '%s %s' % (first_name, last_name)
greet('Jane', 'Smith')
Our call stack would have the greet
function at the bottom and full_name
at the top.
Local and Global Scope
Variables that are inside a function has a local scope. It’s only available inside the function and nested function inside it.
They can’t be referenced outside the function.
Anything at the top level of a Python file has global scope.
Variables with global scope can be accessed inside anything located in a lower function like a block or function.
For example, if we have:
x = 1
def show_x():
print(x)
show_x()
Then we can reference x
inside the show_x
function as x
has global scope.
On the other hand, if we have:
def show_x():
x = 1
print(x)
We’ll get an error saying x
isn’t defined.
Having scopes lets us narrow down the scope of the code that can cause a bug. We don’t have to search through the whole program to troubleshoot everything.
Conclusion
Functions are used to organize code into reusable pieces. They take parameters and we call them by passing in arguments.
They can return something with the return
keyword. Returned values from a function can be assigned to other variables.
Variables inside functions have a local scope. They can’t be accessed from outside the function.
On the other hand, variables at the top level have global scope, which can be accessed inside functions.