CS: Functions and Parameters
Writing reusable code with inputs and return values
CS: Functions and Parameters
Writing reusable code with inputs and return values
CS - Grade 9-12
- 1
In your own words, explain what a function is in programming and why programmers use functions.
Think about how a function can be called more than once.
A function is a named block of code that performs a specific task. Programmers use functions to organize code, avoid repetition, and make programs easier to read, test, and reuse. - 2
What is a parameter? Explain how a parameter is different from an argument.
A parameter is a variable listed in a function definition that receives a value when the function is called. An argument is the actual value passed into the function during the call. - 3
Consider this Python function: def greet(name): return "Hello, " + name. What is the parameter, and what is the return value when the function is called as greet("Maya")?
Look inside the parentheses in the function definition and in the function call.
The parameter is name. When greet("Maya") is called, the return value is "Hello, Maya". - 4
Write a Python function named square that takes one parameter named number and returns number multiplied by itself.
One correct function is: def square(number): return number * number. This function takes the parameter number and returns its square. - 5
Trace the following code and state the final value stored in result: def add_tax(price): return price * 1.08 result = add_tax(50)
Substitute 50 for price inside the function.
The final value stored in result is 54.0 because the function multiplies 50 by 1.08. - 6
A function is defined as def calculate_total(cost, quantity): return cost * quantity. What are the parameters, and what value is returned by calculate_total(7, 4)?
The parameters are cost and quantity. The function call calculate_total(7, 4) returns 28 because 7 multiplied by 4 equals 28. - 7
Explain the difference between printing a value inside a function and returning a value from a function.
A printed value is shown, while a returned value can be reused by the program.
Printing a value displays it to the user, but it does not send the value back to the part of the program that called the function. Returning a value sends the result back so it can be stored, used in an expression, or passed to another function. - 8
Find and fix the error in this function call: def average(a, b): return (a + b) / 2 score = average(80)
The error is that the function average requires two arguments, but only one argument was provided. One correct fix is score = average(80, 90), which provides values for both a and b. - 9
Write a Python function named is_passing that takes one parameter named score and returns True if score is greater than or equal to 60, otherwise returns False.
A comparison expression can produce True or False.
One correct function is: def is_passing(score): return score >= 60. This returns True for scores of 60 or higher and False for scores below 60. - 10
Consider this code: def double(x): return x * 2 def add_three(y): return y + 3 value = add_three(double(5)). What is the final value of value?
The final value of value is 13. First, double(5) returns 10. Then add_three(10) returns 13. - 11
A programmer writes this function: def discount(price, percent): return price - percent. Explain why this function may not correctly calculate a percent discount, and write a better return statement.
A 20 percent discount on 50 dollars should subtract 10 dollars, not 20 dollars.
The function subtracts the percent number directly from the price instead of subtracting that percentage of the price. A better return statement is return price - (price * percent / 100). - 12
Design a function named format_username that takes two parameters, first_name and last_name, and returns a username made from the first three letters of the first name plus the first three letters of the last name, all lowercase. Describe what format_username("Jordan", "Smith") should return.
One correct function is: def format_username(first_name, last_name): return first_name[:3].lower() + last_name[:3].lower(). The call format_username("Jordan", "Smith") should return "jorsmi".