Class XII: Python Functions

By: Archana Shukla and Rajesh Shukla

A function is a group of statements written to perform a specific task.

or

In Python, a function is a group of related statements that perform a specific task.
Functions help break our program into smaller and modular chunks. As our program grows larger and larger, functions make it more organized and manageable.
Furthermore, it avoids repetition and makes code reusable.

Advantages of functions:

  • the function helps us to make the program modular in nature
  • Using functions we can divide a large program into a number of functions/modules.
  • Understanding the program becomes easy
  • Debugging the program becomes easy
  • Reusability of the code increases: function once written can be used again and again as and when required.
  • Using function we can create our own library and later we can use them.
  • Functions are the most important building blocks for any application in python.
  • Functions works on divide and conquer approach to solve a task.
  • A function can be called any number of times.

Syntax of Function

def function_name(parameters):
      """docstring"""
      statement(s)

Above shown is a function definition that consists of the following components.

1.Keyword def marks the start of the function header.
2. A function name to uniquely identify it. Function naming follows the same rules of writing identifiers in Python.
3.Parameters (arguments) through which we pass values to a function. They are optional.
4. A colon (:) to mark the end of the function header.
5. Optional documentation string (doc string) to describe what the function does.
6.One or more valid python statements that make up the function body. Statements must have the same indentation level.
7.An optional return statement to return a value from the function.

Types of Functions

Basically, we can divide functions into the following two types:

1. User-defined functions – Functions defined by the users themselves.
2. Built-in functions – Functions that are built into Python.

Number Of Visitors

0270466