Python for Katy
Functions and Function Parameters
When you define a function in python you use the def
keyword followed by the function name, a set of parenthesis, and a colon.
def some_function():
return
A return value is the thing you want the function to give back to your program.
If you want to be able to "pass" things into your function, you use parameters(also known as arguments) These are variables that only exist within the function and are named inside the parentheses
def some_function(parameter_one, parameter_two):
return
When you call a function, you are using that function to get whatever output you want.
Say we wanted to write a function add
that took two numbers and added them together. We would define it like:
def add(number_one, number_two):
return number_one+number_two
This is a simple function and you likely would never use it because...well you can just add in your code...but to illustrate, when we call add
we would call it like this:
add(2,4)
In this instance, the value 2 would be temporarily stored in the number_one
variable, and the value 4 would be temporarily stored in the number_two
variable. This is how parameters make your functions generic.
if we wanted to add two MORE numbers, we would just do:
add(3,4)
Now 3 would be stored in number_one
and 4 would be stored in number_2
You can store the return value of the function in another variable.
def years_to_dog_years(people_years):
return people_years * 7
hachibooboo_age = years_to_dog_years(2)