Functions and Return Values in Python

In this activity, you’ll get more practice with functions. By including a return value, functions can build upon each other. Those return values can be used elsewhere in the code. Otherwise, we can’t refer to values created within functions outside of them.

Starter file

# Define a function "warble" that takes in a string as an argument, adds " arglebargle" to the end of it, and returns the result.


# Print the result of calling your "warble" function with the argument "hello".


# Define a function "wibble" that takes a string as an argument, prints the argument, prepends "wibbly " to the argument, and returns the result


# Print the result of calling your "wibble" function with the argument "bibbly"


# Define a function "print_sum" that takes in two numbers as arguments and prints the sum of those two numbers.


# Define a function "return_sum" that takes in two numbers as arguments and returns the sum of those two numbers


# Define a function "triple_sum" that takes in 3 arguments and returns the sum of those 3 numbers.


# Define a function "dance_party" that takes in a string as an argument, prints "dance!", updates the string from calling "wibble" function with that argument, updates the string from calling "warble" function with that argument, returns the updated string


# Print the result of calling your "dance_party" function with your name as the argument

Instructions

Open the starter file and perform the following:

  1. Define a function warble that takes in a string as an argument, adds “arglebargle” to the end of it, and returns the result.
  2. Print the result of calling your warble function with the argument “hello”.
  3. Define a function wibble that takes in a string as an argument, prints the argument, prepends “wibbly” to the argument, and returns the result.
  4. Print the result of calling the wibble function with the argument “bibbly”.
  5. Define a function print_sum that takes in two numbers as arguments and prints the sum of those two numbers.
  6. Define a function return_sum that takes in two numbers as arguments and returns the sum of those two numbers.
  7. Define a function triple_sum that takes in three arguments and returns the sum of those three numbers.
  8. Define a function dance_party that:
    • Takes in a string as an argument.
    • Prints “dance!”
    • Updates the string from calling the wibble function with that argument.
    • Updates the string from calling the warble function with that argument.
    • Returns the updated string.
  9. Print the result of calling your dance_party function with your name as the argument. # Should result in: # dance! # MyName # wibbly MyName arglebargle

Solution

# Define a function "warble" that takes in a string as an argument, adds " arglebargle" to the end of it, and returns the result.
def warble(string_param):
    return(f"{string_param} arglebargle")

# Print the result of calling your "warble" function with the argument "hello".
print(warble("hello"))

# Define a function "wibble" that takes a string as an argument, prints the argument, prepends "wibbly " to the argument, and returns the result
def wibble(string_param):
    return(f"wibbly {string_param}")

# Print the result of calling your "wibble" function with the argument "bibbly"
print(wibble("bibbly"))

# Define a function "print_sum" that takes in two numbers as arguments and prints the sum of those two numbers.
def print_sum(num_1, num_2):
    sum = num_1 + num_2
    print(sum)

# Define a function "return_sum" that takes in two numbers as arguments and returns the sum of those two numbers
def return_sum(num_1, num_2):
    sum = num_1 + num_2
    return sum

# Define a function "triple_sum" that takes in 3 arguments and returns the sum of those 3 numbers.
def triple_sum(num_1, num_2, num_3):
    sum = num_1 + num_2 + num_3
    return sum

# Define a function "dance_party" that takes in a string as an argument, that prints "dance!", updates the string from calling "wibble" function with that argument, updates the string from calling "warble" function with that argument, returns the updated string
def dance_party(string_param):
    print("dance!")
    print(string_param)
    print(wibble(warble(string_param)))

# Print the result of calling your "dance_party" function with your name as the argument
dance_party("Andrew")

We will be happy to hear your thoughts

Leave a reply