Lesson 1 of 0
In Progress

Lesson #12: Functions

In the past few lessons, you used the functions print() and len() to display text and determine the length of a string. These are all built-in functions because they come built into the Python language itself. You can also create user-defined functions that execute specific tasks.

A function is a block of code that only runs when it is called. You can pass data, known as parameters, into a function and return data as a result.

Create Your Own Function

In Python, you can define a function using the def keyword.


def addition(x, y):
  result = x + y
  return result

Call Your Own Function

To call a function, use the function name followed by parenthesis with the arguments.


def addition(x, y):
  result = x + y
  return result

x = 1
y = 2
addition(x, y)

Function With No Return Statement

All functions in Python return a value, even if that value is None. However, not all functions need a return statement.


def greetings(name):
  print( “Hey, {name} 👋”)

greetings(“Nick”) # Hey, Nick 👋
greetings(“George”) # Hey, George 👋

Default Parameter Value


def introduction(name = “Nick”):
  print(“My name is “ + name)

introduction(“George”) # My name is George
introduction(“Maria”) # My name is Maria.
# My name is Nick 👋