0
0
Pythonprogramming~5 mins

Function definition and syntax in Python - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the basic syntax to define a function in Python?
Use the def keyword, followed by the function name, parentheses (), and a colon :. Then indent the function body below. Example:
def my_function():
    print("Hello")
Click to reveal answer
beginner
What does the return statement do inside a function?
It sends a value back to where the function was called. This lets the function give back a result. Without return, the function returns None by default.
Click to reveal answer
beginner
Can a Python function have parameters? How are they used?
Yes, parameters are names inside the parentheses in the function definition. They act like placeholders for values you give when calling the function. Example:
def greet(name):
    print(f"Hello, {name}!")
Click to reveal answer
intermediate
What happens if you call a function without parentheses?
You get a reference to the function itself, not the result of running it. To run the function and get its output, you must use parentheses, even if empty.
Click to reveal answer
beginner
Why is indentation important in Python function definitions?
Indentation shows which lines belong inside the function. Python uses indentation instead of braces. Without correct indentation, Python will give an error or run code incorrectly.
Click to reveal answer
Which keyword is used to define a function in Python?
Adefine
Bfunction
Cfunc
Ddef
What will this function return if called without a return statement?
def add(a, b):
    c = a + b
AThe sum of a and b
BNone
CAn error
Da + b as a string
How do you call a function named say_hello with no parameters?
Asay_hello()
Bsay_hello
Ccall say_hello
Ddef say_hello()
What is the purpose of parameters in a function?
ATo return values from the function
BTo store the function's name
CTo hold values passed into the function
DTo indent the function body
Why must the function body be indented in Python?
ABecause Python uses indentation to group code blocks
BTo separate the function from other code
CTo make the code look pretty
DTo add comments inside the function
Explain how to define a simple function in Python that takes one parameter and returns a value.
Think about the parts you need to write a function that gives back a result.
You got /6 concepts.
    Describe why indentation is important in Python function definitions and what happens if it is missing.
    Consider how Python knows which lines belong inside the function.
    You got /4 concepts.