def
return
Define/create/make a function: To set up and write the instructions for a function.
Execute/call/use a function: To actually use the pre-defined function.
Input/parameter/argument: A variable defined by the user that is put/passed in between the parantheses ()
that comes after the function name.
Output: The variable that is return
ed to the user after the function is executed.
Copy + Pasting the same/similar bit of code is to be avoided.
Functions are one way to avoid this.
Loops are another! (we'll get to these soon...)
# you've seen functions before
# here we use the type() function
my_var = 3
type(my_var)
# the function print() doesn't depend on type()
# but they can both be used on the same variable
print(my_var)
When you use def
, you are defining a function.
You are metaphorically writing the instructions for how to make the cheeseburger.
# define a function: double_value
# Notice that the parameter `num` is not explicitly defined with an =
# This is because it will be defined later by the user when they execute the function.
def double_value(num):
# do some operation
doubled = num + num
# return output from function
return doubled
# excecute a function by calling the function's name
# and defining the parameter within the parentheses
double_value(num = 2)
# equivalent function call
# Here the parameter `num` is defined without
# explicitly specifying the name of the parameter
double_value(2)
Here we are defining a function with multiple parameters
def add_two_numbers(num1, num2):
# Do some operations with the parameters
answer = num1 + num2
# Return the variable answer
return answer
add_two_numbers(5, 14)
# Execute our function again on some other inputs to double check
# that our function is working how we think it should
output = add_two_numbers(-1, 4)
print(output)
def
followed by the name of the function, parentheses ()
, parameters within the parentheses, and then :
after the parentheses, which opens a code-block that comprises the functiondef
block defines the function (but does not execute it)()
without def
and :
return
to exit the function, passing out any specified variablesreturn
ed) to a variableGiven the function defined below, what will the second code cell below return?
A) 0 B) 2 C) 4 D) '2r.2 + 1' E) ¯\_(ツ)_/¯
def remainder(number, divider):
r = number % divider
return r
ans_1 = remainder(12, 5)
ans_2 = remainder(2, 2)
print(ans_1 + ans_2)
Write a function greet
that takes the parameter name
. Inside the function, concatenate 'Hello', the person's name, and 'Good morning!". Assign this to output
and return output
.
A) I did it! B) I think I did it. C) I tried but I am stuck. D) Super duper lost.
## YOUR CODE HERE
# TEST YOUR FUNCTION HERE
Specify a default value in a function by doing an assignment within the function definition.
# Create a function, that has a default values for a parameter
def exponentiate(number, exponent=2):
return number ** exponent
# Use the function, using default value
exponentiate(3)
# Call the function, over-riding default value with something else
# python assumes values are in order of parameters specified in definition
exponentiate(2, 3)
# you can always state this explicitly
exponentiate(number=2, exponent=3)
# Positional arguments use the position to infer which argument each value relates to
exponentiate(2, 3)
# Keyword arguments are explicitly named as to which argument each value relates to
exponentiate(number=2, exponent=3)
exponentiate(exponent=3, number=2)
# Note: once you have a keyword argument, you can't have other positional arguments afterwards
# this cell will produce an error
exponentiate(number=2, 3)
Reminder, setting a default value for parameters is allowed during function definition.
(This may look like what we did above, but here we are including a default value for one parameter during function definition. During function execution, you can't mix and match using positional vs. keywords)
def exponentiate(number, exponent=2):
return number ** exponent
What will the following code snippet print?
def exponentiate(number, exponent=2):
return number ** exponent
exponentiate(exponent=3, number=2)
Note: when using Keyword arguments position/order no longer matters
=
def remainder(number, divider=2):
r = number % divider
return r
# could be improved by adding empty lines to separate out logical chunks
def remainder(number,divider=2): # needs space after comma
r=number%divider # needs spacing around operators
return r
# You can check variables defined in the global namespace with `%whos`
%whos
# Names used inside a function are independent of those used outside
# variables defined outside of functions are global variables
# global variables are always available
my_var = 'I am a variable'
print(my_var)
# define a function that uses my_var inside the function
def concat_self(my_var):
my_var = my_var + ' ' + my_var
return my_var
print(concat_self(my_var))
# see that my_var in global name space remains unchanged
print(my_var)
# only way to change my_var in global namespace
# is to assign to variable my_var in global namespace
my_var = concat_self(my_var)
print(my_var)
Write a function convert_to_f
that will convert a temperature in Celsius to Farenheit, returning the temperature in Farenheit.
Note: A temperature in Celsius will be multipled by 9/5 and then 32 will be added to that quantity to convert to Farenheit
A) I did it! B) I think I did it. C) I tried but I am stuck. D) Super duper lost.
## YOUR CODE HERE
# TEST YOUR FUNCTION HERE