DEV Community

yugalakshmi
yugalakshmi

Posted on

Functions in Python-1

Introduction

Before learning Functions, we should know:

  1. Variables / Identifiers
  2. Data Types
  3. Operators
  4. Keywords
  5. Functions
  6. 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.
  1. 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)