Where can functions come from? (3)
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)?
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)?
Can a single integer be iterated through the for loop?
No
What is the output of:
def multiply(a, b):
returnprint(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 Falseprint(isInt(5))
print(isInt(5.0))
print(isInt(“5”))
True
False
None
What is the output of:
def myFunction():
global var
var = 2
print("Do I know that variable?", var)var = 1
myFunction()
print(var)
Do I know that variable? 2
2
What is the output of:
def myFunction(myList1):
print(myList1)
myList1 = [0, 1]
myList2 = [2, 3]
myFunction(myList2)
print(myList2)
[2, 3]
[2, 3]
What is the output of:
def myFunction(myList1):
print(myList1)
del myList1[0]myList2 = [2, 3]
myFunction(myList2)
print(myList2)
[2, 3]
[3]
A variable that exists outside a function has a scope inside the function body unless when?
The function defines a variable of the same name
True or False: A variable that exists inside a function has a scope inside the function body
True