1.1.5 Functions and procedures

    Cards (65)

    • Functions and procedures are blocks of code designed to perform specific tasks
    • Functions always return a value after execution.
    • Procedures are also known as subroutines or void functions
    • Procedures do not return a value.
    • What is the primary use case of a procedure?
      Executes a task without returning a result
    • The main difference between functions and procedures lies in their return value
    • Procedures do not return a value.
    • What is the role of a return statement in a function?
      Sends a value back
    • The name of a function or procedure is a unique identifier
    • Only functions require a return statement.
    • What is an alternative term for a procedure?
      Void function
    • Procedures return a value after execution.
      False
    • A function like `add(x, y)` returns the sum
    • What is the purpose of a procedure like `print_message(text)`?
      Prints a message
    • What is the primary use case of a function?
      Calculates and returns a result
    • A function can add two numbers and return their sum.
    • A procedure might print a message
    • Why is the distinction between functions and procedures important?
      Structured and reusable code
    • Arrange the components of a function or procedure structure.
      1️⃣ Name
      2️⃣ Parameters
      3️⃣ Body
      4️⃣ Return statement (if applicable)
    • The return statement is used in functions
    • What does the following function calculate: \text{def calculate_{a}rea(width, height):} \\ \hspace{1em} \text{area} = \text{width} * \text{height} \\ \hspace{1em} \text{return area}</latex>?
      Area of a rectangle
    • A procedure, also known as a void function, does not return a value.
    • How do you call a function or procedure in code?
      Use its name with parameters
    • To define a function or procedure, use the keyword def
    • What keyword is used to return a value from a function?
      return
    • Procedures do not return a value but can execute tasks.
    • Comparing function and procedure calls, a function returns a value
    • A procedure call does not usually assign its result to a variable.
    • What keyword is used to define functions and procedures in code?
      def
    • Functions return a value using the return statement.
    • Procedures do not return a value.
    • Match the feature with the type of call:
      Returns a value ↔️ Function Call
      No return value ↔️ Procedure Call
    • What is an example of a function that adds two numbers?
      defadd(x,y):returnx+def add(x, y): return x +y y
    • Procedures often use the print statement to display output.
    • Procedures always return a value.
      False
    • What is the purpose of a function in programming?
      Return a calculated value
    • Procedures are also known as void functions or subroutines.
    • Steps to implement functions and procedures in code
      1️⃣ Define the function or procedure using `def`
      2️⃣ Specify parameters in parentheses
      3️⃣ Write the code block
      4️⃣ Use `return` for functions
      5️⃣ Call the function or procedure
    • What is the output of calling `add(5, 3)` in the example provided?
      8
    • Procedures can be assigned to variables.
      False