1 Answers
🧠 Quick Study Guide: Python Procedure Calls
- 🎯 Definition: In Python, procedures are typically referred to as functions. They are blocks of organized, reusable code that perform a single, related action.
- ⚙️ Defining a Function: Use the
defkeyword, followed by the function name, parentheses(), and a colon:. For example,def greet(name):. - 📞 Calling a Function: To execute the code inside a function, you 'call' it by using its name followed by parentheses
(). For example,greet("Alice"). - 🎁 Arguments & Parameters: Parameters are variables listed inside the parentheses in the function definition. Arguments are the actual values passed to the function when it's called.
- 🔢 Types of Arguments: Python supports positional arguments (order matters), keyword arguments (name matters), default arguments (pre-assigned values), and arbitrary arguments (
*argsfor non-keyworded,kwargsfor keyworded). - ↩️ Return Values: Functions can optionally return a value using the
returnstatement. If noreturnstatement is present, orreturnis used without a value, the function implicitly returnsNone. - 🚫 Procedures vs. Functions: While often used interchangeably, strictly speaking, a procedure performs an action without returning a value, whereas a function returns a value. In Python, all 'procedures' are technically functions, and they return
Noneif no explicitreturnvalue is specified.
📝 Practice Quiz: Python Procedure Calls
What is the primary keyword used to define a function in Python?
A)
functionB)
defineC)
defD)
procConsider the function
def calculate_area(length, width): return length * width. Which of the following is a correct way to call this function using keyword arguments?A)
calculate_area(5, 10)B)
calculate_area(length=5, width=10)C)
calculate_area(width=10, 5)D)
calculate_area(5, width=10)What will be the output of the following Python code?
def say_hello(): print("Hello!") result = say_hello() print(result)A)
Hello!followed byHello!B)
Hello!followed byNoneC)
Nonefollowed byHello!D) An error
Which type of argument allows a function to accept an arbitrary number of non-keyworded arguments?
A) Default arguments
B) Keyword arguments
C)
*argsD)
kwargsIf a Python function is defined as
def power(base, exponent=2): return base ** exponent, what willpower(3)return?A) 9
B) 6
C) 27
D) An error
Which of the following statements about Python functions is TRUE?
A) All Python functions must explicitly return a value.
B) Functions can only accept positional arguments.
C) A function without an explicit
returnstatement implicitly returnsNone.D) Function names must start with an uppercase letter.
Given the function definition
def greet(name, message="Hello"): print(f"{message}, {name}!"), which call will print "Hi, Bob!"?A)
greet("Bob")B)
greet(name="Bob", message="Hi")C)
greet("Bob", "Hi")D) Both B and C
Click to see Answers
1. C
2. B
3. B
4. C
5. A
6. C
7. D
Join the discussion
Please log in to post your answer.
Log InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! 🚀