Learn Python 3 - Functions Reference Guide - Codecademy
Learn Python 3 - Functions Reference Guide - Codecademy
Learn Python 3
Functions
Print cheatsheet
Function Parameters
Sometimes functions require input to provide data for their code. This input is
de ned using parameters.
Parameters are variables that are de ned in the function de nition. They are
assigned the values which were passed as arguments when the function was
called, elsewhere in the code.
Multiple Parameters
Python functions can have multiple parameters. Just as you wouldn’t go to
school without both a backpack and a pencil case, functions may also need
more than one input to carry out their operations.
Python Functions
Some tasks need to be performed multiple times within a program. Rather than
rewrite the same code in multiple places, a function may be de ned using the
def keyword. Function de nitions may include parameters, providing data
input to the function.
Functions may return a value using the return keyword followed by the value
to return.
def my_function(x):
return x + 1
print(my_function(2)) # outputs: 3
print(my_function(3 + 5)) # outputs: 9
Function Indentation
Python uses indentation to identify blocks of code. Code within the same block
should be indented at the same level. A Python function is one type of code
block. All code under a function declaration should be indented to identify it as
part of the function. There can be additional indentation within a function to
handle other statements such as for and if so long as the lines are not
indented less than the rst line of the function code.
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 2/7
10/03/2020 Learn Python 3: Functions Reference Guide | Codecademy
def testfunction(number):
# This code is part of testfunction
print("Inside the testfunction")
sum = 0
for x in range(number):
# More indentation because 'for' has a code block
# but still part of he function
sum += x
return sum
print("This is not part of testfunction")
Calling Functions
Python uses simple syntax to use, invoke, or call a preexisting function. A
function can be called by writing the name of it, followed by parentheses.
For example, the code provided would call the doHomework() method.
doHomework()
Function Arguments
Parameters in python are variables — placeholders for the actual values the
function needs. When the function is called, these values are passed in as
arguments.
For example, the arguments passed into the function .sales() are the “The
Farmer’s Market”, “toothpaste”, and “$1” which correspond to the parameters
grocery_store , item_on_sale , and cost .
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 3/7
10/03/2020 Learn Python 3: Functions Reference Guide | Codecademy
findvolume(1, 2, 3)
findvolume(length=5, depth=2, width=4)
findvolume(2, depth=3, width=4)
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 4/7
10/03/2020 Learn Python 3: Functions Reference Guide | Codecademy
In the example, the variable a is de ned both inside and outside of the
function. When the function f1() is implemented, a is printed as 2 because
it is locally de ned to be so. However, when printing a outside of the function,
a is printed as 5 because it is implemented outside of the scope of the
function.
a = 5
def f1():
a = 2
print(a)
def check_leap_year(year):
if year % 4 == 0:
return str(year) + " is a leap year."
else:
return str(year) + " is not a leap year."
year_to_check = 2018
returned_value = check_leap_year(year_to_check)
print(returned_value) # 2018 is not a leap year.
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 5/7
10/03/2020 Learn Python 3: Functions Reference Guide | Codecademy
Global Variables
A variable that is de ned outside of a function is called a global variable. It can
be accessed inside the body of a function.
a = "Hello"
def prints_a():
print(a)
Like local variables, parameters cannot be referenced from outside the scope of
the function.
def my_function(value):
print(value)
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 6/7
10/03/2020 Learn Python 3: Functions Reference Guide | Codecademy
https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-functions/cheatsheet 7/7