Writing functions with multiple inputs and outputs - SOLVED
Contents
Writing functions with multiple inputs and outputs - SOLVED#
You’ve now written a number of functions. These functions all took a single input value, and returned a single output value.
However, functions can take an arbitrary number of inputs (including 0) and return an arbitrary number of outputs (including 0).
Let’s illustrate how this works, and then give you some exercises to practice!
Two or more inputs#
The function below takes two inputs x and y and returns their sum (x + y). Note that in the def line there are two values within the input brackets, separated by a comma:
def g(x, y):
"""Takes two inputs, x and y, and returns their sum"""
return x + y
This function can then be called by sending it two values. These can be given (numeric) values, or variables:
# Using numeric values
print(g(5,7)) # Computes and prints 5 + 7
# Using variables
a = 5
b = 7
print(g(a, b)) # Also computes and prints 5 + 7
# Again using variables, but now we store the output
a = 5
b = 7
c = g(a, b) # Computes 5 + 7
print (c) # Prints the output. Note in this case, we could use c in future computations
12
12
12
Two or more outputs#
The function below takes one input value x and returns \(x^2\) and \(x^3\). Note in this case there are two values on the return line, separated by a comma
def output_func(x):
"""Takes one inputs, x and returns x^2 and x^3"""
b = x**2
c = x**3
return b, c
print(output_func(4))
(16, 64)
Note that both values are returned. We can store the output by doing something like:
def output_func(x):
"""Takes one inputs, x and returns x^2 and x^3"""
b = x**2
c = x**3
return b, c
out1, out2 = output_func(4)
print(f"For an x of {4} x^2 is {out1} and x^3 is {out2}")
For an x of 4 x^2 is 16 and x^3 is 64
Exercise#
Write a function that takes one input x and returns \(\sqrt{x}\) and \(x^5\) (in that order).
def exercise1(x):
"""Return x**0.5 and x**5"""
return x**0.5, x**5
# Run this cell to test if your function above works. Change the first line to match the name of your function
fn_handle = exercise1
# I'm now going to use some stuff from further in the lecture, and another function, to test this.
# the "isclose" function tests if two numbers are almost equal. Good when dealing with floating point numbers.
import math
bbb, ccc = fn_handle(4) # Call the function with an input value of 4
assert (math.isclose(bbb, 2.0) and math.isclose(ccc, 1024))
bbb2, ccc2 = fn_handle(-3)
# This one is complex, so more involved checking needed.
assert (math.isclose(bbb2.real, 1.0605752387249068e-16) and
math.isclose(bbb2.imag,1.7320508075688772) and
math.isclose(ccc2, -243))
bbb3, ccc3 = fn_handle(11)
assert(math.isclose(bbb3, 3.3166247903554) and math.isclose(ccc3, 161051))
print("Congratulations, it looks like your function works!")
Congratulations, it looks like your function works!
Exercise#
Write a function that takes two inputs x and y and return their product (\(x \times y\))
def exercise2(x, y):
"""Returns x * y"""
return x*y
# Run this cell to test if your function above works. Change the first line to match the name of your function
fn_handle = exercise2
bbb = fn_handle(4, 10) # Call the function with an input value of 4
assert (bbb == 40)
bbb2 = fn_handle(-3, 18)
assert (bbb2 == -54)
bbb3 = fn_handle(11, -1)
assert(bbb3 == -11)
print("Congratulations, it looks like your function works!")
Congratulations, it looks like your function works!
Exercise#
Write a function that takes two inputs x and y (in that order) and returns two outputs. The two outputs should be \(x + y\) and \(x \times y\) (in that order).
def exercise3(x, y):
"""returns x+y and x*y"""
return x+y, x*y
exercise3(6,7)
(13, 42)
# Run this cell to test if your function above works. Change the first line to match the name of your function
fn_handle = exercise3
# I'm now going to use some stuff from further in the lecture, and another function, to test this.
# the "isclose" function tests if two numbers are almost equal. Good when dealing with floating point numbers.
import math
bbb, ccc = fn_handle(4,10) # Call the function with an input value of 4
assert(bbb == 14 and ccc == 40)
bbb2, ccc2 = fn_handle(-3,18)
# This one is complex, so more involved checking needed.
assert (bbb2 == 15 and ccc2 == -54)
bbb3, ccc3 = fn_handle(11,-1)
assert(bbb3 == 10 and ccc3 == -11)
print("Congratulations, it looks like your function works!")
Congratulations, it looks like your function works!