CS Grade 9-12

CS: Functions and Parameters

Writing reusable code with inputs and return values

View Answer Key
Name:
Date:
Score: / 12

Writing reusable code with inputs and return values

CS - Grade 9-12

Instructions: Read each problem carefully. Show your work in the space provided.
  1. 1
    Diagram of data entering a reusable function module and transformed output leaving it.

    In your own words, explain what a function is in programming and why programmers use functions.

  2. 2
    Diagram showing an empty placeholder in a function and a supplied value fitting into it.

    What is a parameter? Explain how a parameter is different from an argument.

  3. 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")?

  4. 4

    Write a Python function named square that takes one parameter named number and returns number multiplied by itself.

  5. 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)

  6. 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)?

  7. 7
    Diagram contrasting displayed output with returned data passed to another function.

    Explain the difference between printing a value inside a function and returning a value from a function.

  8. 8

    Find and fix the error in this function call: def average(a, b): return (a + b) / 2 score = average(80)

  9. 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.

  10. 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?

  11. 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.

  12. 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.

LivePhysics™.com CS - Grade 9-12

More CS Worksheets

See all CS worksheets

More Grade 9-12 Worksheets

See all Grade 9-12 worksheets