All Flashcards
What are the steps for calling a procedure?
- The program encounters the procedure call. 2. The program jumps to the procedure's code. 3. The procedure executes its code with the provided arguments. 4. If a
RETURNstatement is encountered, the program gets the returned value. 5. The program continues from where it left off.
What are the steps to define and use a procedure?
- Define the procedure with a name, parameters, and code statements. 2. Use the
RETURNstatement to send back a value (if needed). 3. Call the procedure by its name, providing arguments for the parameters. 4. Use or store the returned value (if any).
What are the differences between parameters and arguments?
Parameters: Variables in the procedure definition (placeholders). Arguments: Actual values passed to the procedure when it is called.
What are the differences between printing a value and returning a value from a procedure?
Printing: Displays the value to the console. Returning: Sends the value back to the calling code for further use.
What does the following code output?
python
def my_procedure(x):
return x * 2
print(my_procedure(5))
10
What does the following code output?
python
def greet(name):
print("Hello, " + name)
greet("Alice")
print(greet("Bob"))
Hello, Alice Hello, Bob None
Identify the error in the following code:
python
def add(x, y):
print(x + y)
result = add(3, 4) + 5
print(result)
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. The add function prints the sum but does not return a value, so result becomes None.
What does the following code output?
python
def calculate(x, y):
z = x * y
return z
result = calculate(4, 5)
print(result + 2)
22
What does the following code output?
python
def my_func(a, b):
a = a + 10
return a + b
x = 5
y = 2
print(my_func(x, y))
print(x)
17 5
Identify the error in the following code:
python
def square(x):
return x * x
print(square)
The code will print the function object itself, not the result of the function. To see the result, you need to call the function with an argument, e.g., print(square(5)).
What does the following code output?
python
def greet(name):
return "Hello, " + name + "!"
message = greet("Charlie")
print(message)
Hello, Charlie!
What does the following code output?
python
def combine(str1, str2):
result = str1 + " " + str2
return result
print(combine("Open", "AI"))
Open AI
Identify the error in the following code:
python
def divide(x, y):
return x / y
print(divide(10, 0))
ZeroDivisionError: division by zero. Dividing by zero is mathematically undefined and will cause a runtime error.
What does the following code output?
python
def outer_function(x):
def inner_function(y):
return x + y
return inner_function(5)
print(outer_function(10))
15