# define and call functiondef greet(): print("Hello!")greet()#ans: Hello!
# function with one parameterdef greet_person(name): print(f"Hello, {name}!")greet_person("Alice")#ans: Hello, Alice!
# multiple parametersdef add(a, b): return a + bresult = add(5, 3)#ans: result is 8
# return valuedef square(x): return x ** 2result = square(5)#ans: result is 25
# no return (returns None)def print_message(msg): print(msg)result = print_message("Hi")#ans: Hi (printed)#ans: result is None
# what is returned?def func(): x = 5func()#ans: None (no return statement)
# what is result?def add(a, b): return a + bresult = add(10, 20)#ans: 30
# function call without ()?def greet(): return "Hello"x = greet#ans: x is the function object, not "Hello"
# what happens?def multiply(x, y): return x * ymultiply(5, 3)#ans: 15 (but not stored)
# can you call before defining?result = add(1, 2)def add(a, b): return a + b#ans: NameError (function not defined yet)
# function with print vs return?def func1(): print(5)def func2(): return 5x = func1()y = func2()#ans: 5 (printed)#ans: x is None, y is 5
# nested function call?def double(x): return x * 2result = double(double(5))#ans: 20
# what is printed?def test(): return 5 print("After return")test()#ans: 5 (return exits function, print never runs)
# function name case?def MyFunction(): return 1myfunction()#ans: NameError (names are case-sensitive)
# redefining function?def func(): return 1def func(): return 2func()#ans: 2 (second definition overwrites first)
Google tag (gtag.js)