Functions Cheatsheet - Codecademy
Functions Cheatsheet - Codecademy
Functions
Function Parameters
Sometimes functions require input to provide data for their code. def write_a_book(character, setting,
This input is defined using parameters.
special_skill):
Parameters are variables that are defined in the function
definition. They are assigned the values which were passed as print(character + " is in " +
arguments when the function was called, elsewhere in the code. setting + " practicing her " +
For example, the function definition defines parameters for a
special_skill)
character, a setting, and a skill, which are used as inputs to write
the first sentence of a book.
Multiple Parameters
Python functions can have multiple parameters. Just as you def ready_for_school(backpack, pencil_case):
wouldn’t go to school without both a backpack and a pencil case,
if (backpack == 'full' and pencil_case ==
functions may also need more than one input to carry out their
operations. 'full'):
To define a function with multiple parameters, parameter names print ("I'm ready for school!")
are placed one after another, separated by commas, within the
parentheses of the function definition.
Functions
Some tasks need to be performed multiple times within a # Define a function my_function() with
program. Rather than rewrite the same code in multiple places, a
parameter x
function may be defined using the def keyword. Function
definitions may include parameters, providing data input to the
function. def my_function(x):
Functions may return a value using the return keyword followed
return x + 1
by the value to return.
print(my_function(2)) # Output: 3
print(my_function(3 + 5)) # Output: 9
Function Indentation
Python uses indentation to identify blocks of code. Code within # Indentation is used to identify code blocks
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 def testfunction(number):
function. There can be additional indentation within a function to # This code is part of testfunction
handle other statements such as for and if so long as the lines
print("Inside the testfunction")
are not indented less than the first line of the function code.
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
Function Arguments
Parameters in python are variables — placeholders for the actual def sales(grocery_store, item_on_sale, cost):
values the function needs. When the function is called, these
print(grocery_store + " is selling " +
values are passed in as arguments.
For example, the arguments passed into the function .sales() item_on_sale + " for " + cost)
are the “The Farmer’s Market”, “toothpaste”, and “$1” which
correspond to the parameters grocery_store , item_on_sale ,
sales("The Farmer’s Market", "toothpaste",
and cost .
"$1")
Function Keyword Arguments
Python functions can be defined with named arguments which def findvolume(length=1, width=1, depth=1):
may have default values provided. When function arguments are
print("Length = " + str(length))
passed using their names, they are referred to as keyword
arguments. The use of keyword arguments when calling a function print("Width = " + str(width))
allows the arguments to be passed in any order — not just the print("Depth = " + str(depth))
order that they were defined in the function. If the function is
return length * width * depth;
invoked without a value for a specific argument, the default value
will be used.
findvolume(1, 2, 3)
findvolume(length=5, depth=2, width=4)
findvolume(2, depth=3, width=4)
Python functions are able to return multiple values using one def square_point(x, y, z):
return statement. All values that should be returned are listed
x_squared = x * x
after the return keyword and are separated by commas.
In the example, the function square_point() returns y_squared = y * y
x_squared , y_squared , and z_squared . z_squared = z * z
# Return all three values:
return x_squared, y_squared, z_squared
year_to_check = 2018
returned_value = check_leap_year(year_to_check)
print(returned_value) # 2018 is not a leap
year.
Global Variables
Print Share