Write function header with function name and parameters (must include () even without parameters)
Optional -- Set default values for parameters
Write function body
Distinguish between global and local variables based on indentation
Determine what to return, if anything, to call site
Call function with appropriate arguments
ExercisesExercises
1.
Q1: Enter the output of the following program or enter “invalid” if the statement would result in an error.
def first_func(a, b):
return a + b * c
def second_func(b, c):
return b / c
a = 10
b = 2
c = 6
print(first_func(a, second_func(b, c)))
2.
Q2: How many times will the function add_variables execute in the following program?
def add_variables(a, b):
return a + b
numbers = [6, 1, -2, 0, 2, 5, -3]
sum = 0
for number in numbers:
sum = add_variables(sum, number)
if sum > 10:
break
print(sum)
3.
Q3: Enter the output of the following program or enter “invalid” if the statement would result in an error.
Q4: Enter the output of the following program or enter “invalid” if the statement would result in an error.
def multiply(value):
return value * 2
def subtract_and_multiply(value):
return multiply(value - 4)
def divide(value):
return value // 5
val = 0
for i in range(5):
if i % 3 == 0:
val = multiply(val)
elif i % 3 == 1:
val = divide(val)
else:
val = subtract_and_multiply(val)
print(val)
5.
Q5: Put the code in the right order to create a program that prints all prime numbers between 1 and 30 (inclusive).
def is_prime(number):
---
for i in range(2, number):
---
if number % i == 0:
---
return False
---
return True
---
for i in range(1, 31):
---
if is_prime(i):
---
print(i, end=" ")