Function in Python Class 12 Computer Science

Working with Function in Python Class Computer Science gives you complete insight of built in functions, user defined functions and functions in module. This Python Function Class 12 Notes is very handy for all those students learning Function in Python Class 12 Computer Science.

What is Function?

  • Function is named set of code written to carry out a specific task.
  • It can be used repeatedly at different places within a program.
  • We can define as many functions as desired in program.

Advantages of function

  • Increases Code reusability
  • Reduces program complexity
  • Increases readability of programs
  • Reduces chances of errors in program
  • Eases program updation

Types of function

  • Built in function
  • Functions in module
  • User defined function

User defined function

  • The items enclosed in “[ ]” are called parameters and they are optional.
  • Function may or may not return a value.
  • Function header always ends with a colon (:).
  • Function name should be unique. Rules for naming identifiers also apply for function naming.
  • The statements outside the function indentation are not considered as part of the function.

Syntax:

def <functionname> ([parameters]):

                statements
                —-,,——–
                return <value>

example:

create a user defined function to display a line like “——————–“ (20 dashes)

Create a user defined function ‘area’ to calculate and display area of rectangle.

Arguments and Parameters
  • An argument is a value passed to the function during the function call which is received in corresponding parameter defined in function header.
  • The no of arguments in calling and called function should be same.

Example

Write a function to find factorial of a number, where number is passed as an argument.

Write a function to calculate average of five numbers stored in a list.

Default parameter
  • Python allows assigning a default value to the parameter.  A default value is a value that is pre-decided and assigned to the parameter when the function call does not have its corresponding argument.
  • The default parameter must be trailing parameters in the function header.

Example:

Write a function power to find x to the power y. if y is not inputted square of x should be calculated.

Function returning a value
  • A function may return a value when called.
  • Python uses ‘return’ keyword to return the value(s) from the function.

Example:

Write a function to find and return largest among ten numbers stored in a list.

Built in function

Functions in Modules

Python offers many built in modules. Some of most commonly used modules are listed below:

  • Math module
  • Random module
  • Statistics module

How to use module

to import specific functions of given module ‘from’ statement can be used as given below

Flow of execution

Scope of variable

  • Scope of variable refers to accessibility scope of a variable within a program or part of a program.
  • A variable can have either local or global scope.

Local variable

  • A variable that is defined inside any function or block is called local variable.
  • It can be accessed only in the function or a block where it is defined.
  • It exists only till the function executes.

Global variable

  • A variable that is defined outside any function or any block is known as a global variable.
  • It can be accessed in any function defined onwards.
  • Any change made to global variable is permanent and affect all the functions where it is used.
  • if you want to use modified value of global variable outside the function, then the keyword ‘global’ should be prefixed to the variable name in the function.

Example:

Program to define and access Global variable outside of function

Example: Accessing modified value of global variable outside function

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!