Function Basics

Length: 00:06:39

Lesson Summary:

Being able to write code that we can call multiple times without repeating ourselves is one of the most powerful things that we can do. Let’s learn how to define functions in Python.

Python Documentation For This Video

Function Basics

We can create functions in Python using the following:

  • The def keyword
  • The function name - lowercase starting with a letter or underscore (_)
  • Left parenthesis (()
  • 0 or more argument names
  • Right parenthesis ())
  • A colon :
  • An indented function body

Here’s an example without an argument:

>>> def hello_world():
...     print("Hello, World!")
>>> hello_world()
Hello, World!

If we want to define an argument we will put the variable name we want it to have within the parentheses:

>>> def print_name(name):
...     print(f"Name is {name}")
>>> print_name("Keith")
Name is Keith

Let’s try to assign the value from print_name to a variable:

>>> output = print_name("Keith")
Name is Keith
>>> output

Neither of these examples has a return value, but we will usually want to have a return value unless the function is our “main” function or carries out a “side-effect” like printing. If we don’t explicitly declare a return value, then the result will be None.

We can declare what we’re returning from a function using the return keyword:

>>> def add_two(num):
...     return num + 2
>>> result = add_two(2)
>>> result

This lesson is only available to Linux Academy members.

Sign Up To View This Lesson
Or Log In

Looking For Team Training?

Learn More