Mod 4 Flashcards
Where can functions come from? (3)
- Built in functions from Python (for example print())
- Python’s preinstalled modules have functions
- The user can write their own functions
Write the code that builds a function named fun and prompts the user to input their name, and then thanks them and prints the name. Make sure to invoke the function.
def fun(): a = input('What is your name?') print('Thanks', a)
fun()
What are the rules about functions (2)?
- Cannot invoke a function which is not known at the moment of invocation
- Cannot have a function and a variable of the same name
Where in the function is a parameter defined?
def function(parameter) inside the parenthesis
What is the difference between a parameter and argument?
Parameters exist inside functions
arguments exist outside functions
what is the output of the below code:
def message(number): print("Enter a number:", number)
message()
TypeError: message() missing 1 required positional argument: ‘number’
What type of parameters are in the below function? def myFunction(a, b, c): print(a, b, c)
myFunction(1, 2, 3)
positional parameters
What type of parameters are in the below function? def introduction(firstName, lastName): print("Hello, my name is", firstName, lastName)
introduction(firstName = "James", lastName = "Bond") introduction(lastName = "Skywalker", firstName = "Luke")
keyword parameters
What is the output of: def intro(a, b="Bond"): print("My name is", b + ".", a + ".")
intro(“Susan”)
My name is Bond. Susan.
What is the output of: def sum(a, b=2, c): print(a + b + c)
sum(a=1, c=3)
SyntaxError - a non-default argument (c) follows a default argument (b=2)
What is the output of: def subtra(a, b): print(a - b)
subtra(5, b=2)
subtra(a=5, 2)
3
Syntax Error positional argument follows a keyword argument
What does the “return” function do? What happens to the code after the return function? What if nothing is assigned to it?
It ends the execution of the function and returns the result. Everything after the return statement is not executed. If a value isnt assigned to the return instruction, it returns “None”
“None” facts/rules (3) and when can it be used (2)?
- it doesnt hold a value
- it must not be used in an expression
- It is a keyword
- when it is assigned to a variable
- when it is compared to a variable
Can a single integer be iterated through the for loop?
No
What is the output of: def multiply(a, b): return
print(multiply(3, 4))
None
What is the output of: def wishes(): print("My Wishes") return "Happy Birthday"
wishes()
My Wishes
What is the output of: def wishes(): print("My Wishes") return "Happy Birthday"
print(wishes())
My Wishes
Happy Birthday
What is the output of: def hi(): return print("Hi!")
hi()
None
What is the output of: def isInt(data): if type(data) == int: return True elif type(data) == float: return False
print(isInt(5))
print(isInt(5.0))
print(isInt(“5”))
True
False
None