Working with functions in python class 12 computer science 083

Working with Function in Python

        A function is a subprogram  that act as on data and return the result. Functions improves a program's clarity and readability and makes programming more efficient by reducing code duplication and breaking down complex task into more manageable small parts.

Characteristics of Function

  • A function is a block of code that performs a specific task.
  • A function is a block of code which only runs when it is called.
  • A function is a block of organized, reusable code that is used to perform a single, related action.
  • A function is simply a “part” of code that you can use over and over again, rather than writing it out multiple times.

Functions enable programmers to break down or decompose a problem into smaller parts, each of which performs a particular task.

Functions also known as routines, sub-routines, methods, procedures and sub-programs.

Types of Function:

  1. Built - In Function
  2. Functions based on Module
  3. User defined Function

1.Built - In Function

Built -in functions are the predefined functions that are already available in Python.

For Example:

input(), print(), int(), float(), eval(), max() and min()

abs(), type(), len(), round(), range()

2. Functions based on Module

When a program become more lengthy and complex, there arises a need for the tasks to be spilt into smaller segments called modules.

When we break a program into modules, each module should contain functions that perform related tasks.

A module in Python is a file that contains a collection of related functions.

Functions of math module:

ceil(), floor(), pow(), sqrt(), fabs(), cos(), sin(), tan()

Functions of random module:

randrange(), randint(), random()

3.User defined Function

A user defined function is created or defined by the def keyword, followed by the function name, parentheses () and colon (:)

Syntax of Python Function 

               def function_name (optional parameters ):

body of the function

               function_name(optional parameters values)

  • In Python every function defined by def keyword.
  • Function must be called/invoked to execute it code.
  • A function must be defined before the function calling.
  • Every function return a value using the return statement as the last statement of the function code.

If a function does not have any return statement then it will return None

The first line of function definition i.e. def function_name (optional parameters ): is called function header.

function_name (optional parameters values) statement is called function calling statement

For Example:

def greetings():

         print("Good Morning")

greetings()

Here def greetings(); is called function header, print("Good Morning") is code of the function and greetings() is function calling statement.

Related Search: (Keywords)

Post a Comment

If you any doubts, Please let me know

Previous Post Next Post