Creating Functions

Python

Define a Function

Creating Functions

Functions are reusable blocks of code that perform a specific task.

Syntax

def function_name(parameter1, parameter2):
    # code here
    return result

Example

def greet(name):
    return f"Hello, {name}!"

message = greet("Alice") print(message) # Hello, Alice!

Your Task

Create a function called calculate_total that:

  • Takes two parameters: price and quantity
  • Returns the product (price × quantity)

    Test it by calling the function with price=25 and quantity=4, then print the result.

  • Code Editor
    Loading PYTHON engine...
    Loading...
    Loading Python engine...

    Run your code to see output