Introduction
Before learning Functions, we should know:
- Variables / Identifiers
- Data Types
- Operators
- Keywords
- Functions
- What is a Function? A function is a set of instructions or a block of code used to perform a specific task.
In simple words, a function is a task or job.
Examples:
- Attending a class
- Playing cricket
- Adding two numbers
- Sending a message Example: Playing Cricket Steps involved in playing cricket:
- Invite friends.
- Take a bat and ball.
- Go to the ground.
- Toss.
- Start batting or fielding. These steps together perform one task: Playing Cricket.
- How to Create a Function in Python?
We use the def keyword to define a function.
Syntax:
def function_name():
statements
Example:
def play():
print("Calling friends")
print("Taking bat and ball")
print("Going to ground")
print("Playing cricket")
Here:
- def is a keyword used to define a function.
- play is the function name.
- () represents the function parameters.
- Indentation represents the function body.
Top comments (0)