If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.
Functions
Functions are reusable chunks (blocks) of code. In order to execute a function, you must call it.
Syntax
# Declaring a function
def function_name():
code_goes_here
# Calling a function
function_name()
Function without Parameters
def generate_full_name ():
first_name = 'Vicki'
last_name = 'Langer'
space = ' '
full_name = first_name + space + last_name
print(full_name)
generate_full_name () # call the function
>>> Vicki Langer
If a function does not return values the value of the function is None. Using the above example, we can adjust it to return
values.
# syntax
def generate_full_name ():
first_name = 'Vicki'
last_name = 'Langer'
space = ' '
full_name = first_name + space + last_name
return full_name
print(generate_full_name ()) # call the function
>>> Vicki Langer
Functions with a Single Parameter
# Declaring a function
def function_name(parameter):
code_goes_here
# Calling a function
function_name(parameter)
Example without math
def greetings (name):
message = name + ', is writing the "Charming the Python" series on Dev.to'
return message
print(greetings('Vicki'))
Obligatory math example
def add_ten(num):
ten = 10
return num + ten
print(add_ten(90)) # will print '100' which is 90 + 10
Functions with Multiple Parameters
A function can also take multiple parameters
def generate_full_name (first_name, last_name):
space = ' '
full_name = first_name + space + last_name
return full_name
print(generate_full_name('Vicki','Langer'))
def sum_two_numbers (num_one, num_two):
sum = num_one + num_two
return sum
print(sum_two_numbers(1, 9)) # will print '10' which is 1 + 9
Functions can even take key/value pairs. In this case, the order of the parameters doesn't matter.
def sum_two_numbers (num_one, num_two):
sum = num_one + num_two
return sum
print(sum_two_numbers(num_two=1, num_one=9)) # will print '10' which is 9 + 1
Functions with Default Parameters
Using the above greetings
example, I'll give it a default parameter of name = 'Vicki'
def greetings (name = 'Vicki'): # this parameter defines the default
message = name + ', is writing the "Charming the Python" series on Dev.to'
return message
print(greetings()) # This prints the default value, as defined in the function parameters
>>> Vicki is writing the "Charming the Python" series on Dev.to
print(greetings('V1ck1')) # this overrides the default and uses the input parameter
>>> V1ck1 is writing the "Charming the Python" series on Dev.to
Unknown Amount of Parameter Arguments
Sometimes, we don't know how many parameters/arguments are needed. To do this, we put *
in front of the argument name.
def feed_pets(*pets):
for pet in pets:
give_food
give_water
print(pet + " is fed and has water")
else:
print('no more pets to feed')
feed_pets('Puppy', "Cheeto", 'Remmy', 'Wiley', "Ruger", "Stick") # call function to feed and water any amout of pets
Another obligatory math example
def sum_numbers(*numbers):
total = 0 # setting the baseline, we start at zero
for number in numbers: #
total += number # current total + next argument =
return total # the new total is
print(sum_numbers(1, 7, 2)) # prints sum of these numbers
print(sum_numbers(1, 2)) # prints sum of these numbers
>>> 10
>>> 3
Series based on
Top comments (0)