Question
1. Types and Functions Let us revisit the type function once again! Use the Python Console in PyCharm, or the IDLE shell. >>> type(1) >>>
1. Types and Functions Let us revisit the type function once again! Use the Python Console in PyCharm, or the IDLE shell.
>>> type(1)
That looks good! Now, let's define a couple of simple functions, hello and ciao. The functions are already defined below, but you need to write the docstring!
def hello(firstname): ''' TODO: Write the docstring! ''' print("Hello, "+firstname+"!") return None def ciao(firstname): ''' TODO: Write the docstring! ''' print("Ciao, "+firstname+"!") return None
Now, lets run the above file, and test out their type in the console!
>>> type(hello)
Functions are also python objects of function type! They also have different properties (one such property is __name__) and can be assigned to variables.
>>> hello1=hello >>> hello1("Apple") Hello, Apple! >>> hello1.__name__ 'hello'
2. Passing Functions as Arguments (Of Other Functions) Now, you have all the tools required to write a function greeting with two parameters: f, a function, and s, a string. Function greeting should call function f(s). Function greeting will return None. You should get the following output!
>>> greeting(hello, 'Orange') Hello, Orange! >>> greeting(ciao, 'Kiwi') Ciao, Kiwi!
Now, modify the function greeting to print the name of the function greeting has received in a parameter.
>>> greeting(hello, 'Orange') Calling hello Hello, Orange! >>> greeting(ciao, 'Kiwi') Calling ciao Ciao, Kiwi!
3. Add or Mult Write a function add_3 that takes as parameters three numbers, a, b, and c, and returns a + b + c. Write a second function add_3 that takes as parameters three numbers, a, b, and c, and returns a * b * c (we will see more general functions once we study lists.) Now, write a function higher_order that takes as parameters one function, f, and three numbers, a, b, and c. Function higher_order must call the received function, f, using as arguments the other received parameters, and return the value by computed f. Also, make function higher_order print the name of the function it received and the other arguments. Its output must look as illustrated below.
>>> higher_order(add_3, 1, 2, 3)
function:add_3
add_3(1, 2, 3) = 6
6
>>> higher_order(mult_3, 1, 2, 3)
function: mult_3
mult_3(1, 2, 3) = 6
Functions that take functions as arguments are called higher-order functions, hence the name of the required function. Save these functions in a file called functions.py. Comment with your peers how much you could do and anything that you found particularly interesting, useful, confusing, or surprising. Feel free to ask questions on Discord or during lectures at any time after lab regarding What happens when a function is called (regarding scope and namespaces) Revising programs/functions The mechanism used by higher-order functions The contents of this document may change according to students' questions or suggestions.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started