0% found this document useful (0 votes)
32 views7 pages

FUNCTION

The document discusses functions in Python including defining functions, parameters, arguments, returning values, scope, and lifetime. Functions are defined as reusable blocks of code that perform tasks. Functions can take parameters, return values, and have different scopes for variables.

Uploaded by

Prajjwal mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views7 pages

FUNCTION

The document discusses functions in Python including defining functions, parameters, arguments, returning values, scope, and lifetime. Functions are defined as reusable blocks of code that perform tasks. Functions can take parameters, return values, and have different scopes for variables.

Uploaded by

Prajjwal mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

FUNCTIONS

A function is defined as a group of statements that can be invoked from any part of the
program for the purpose of performing a specific task.
Alternative names
Function /Method/Procedure/Routine/Sub-routine/Sub-program.
Example

Advantages of using a function


1. Reduces duplication of code, which results into small program size.
2. It makes the code more efficient.
3. Enhances reusability of the code.
4. It makes debugging easy.
5. Improves clarity and readability of the code.
Way of Working
Functions are used to solve the problem by dividing the given problem into several sub-
problems, finding their individual solutions and integrating the solutions of individual
problems to solve the original problems. This approach of problem solving is called
stepwise refinement method/modular approach/divide and conquer approach.

Calling / Invoking / Using a function : To use a function, we need to write a function


call statement.
 calcSomething(5)
 a=7
calcSomething(a)
 a=int(input(‘Enter a number : ’))
calcSomething(a)
 print(calcSomething(5))
 a=2* calcSomething(5)
Python Function Types
1. Built-in functions : These are pre-defined functions and are always available for use.
Example : len( ), type( ), int( ), input( ) etc.
2. Functions defined in modules : These functions are pre-defined in particular
modules an can only be used when the corresponding module is imported.
Example : If you want to use sin( ), you need to import the module math in your
program.
3. User defined functions : These are defined by the programmer as per the requirement.

Defining Functions in Python

Flow of Execution in a Function Call

Flow of execution refers to the order in which statements are executed during a program
run.

Important Points to Remember


 Comment lines are ignored.
 If the called function returns a value, then the control will jump back to the
function call statement and completes it.
 If the called function does not return any value i.e the return statement has no
variable or value or expression, then the control jumps back to the line following
the function call statement.
267823489

1567128

 Determining flow of execution on paper is known as Tracing the program.


 The function which calls another function is called the caller and the function
being called is the called function or callee.
Arguments and Parameter
 Argument is a value that is passed to a function.
 Parameter is a variable defined by a function
that receives a value.
Arguments appear in function call statement and
Parameters appear in function header.
Argument / Actual Argument / Actual Parameter
Parameter / Formal parameter / Formal Argument

Types of Parameters : Python supports three types of parameters :


 Positional Argument / Required Argument / Mandatory Arguments : The
function call statement must match the number and order of arguments as defined
in the function definition.
Example :

 Default Argument : A parameter having default value in the function header is


known as a default parameter. They are useful in situations where some
parameters always have the same value.
Example :
NOTE : Non – Default parameters cannot follow default arguments.

ERROR
 Keyword / Named Argument : In keyword argument, we can write any argument
in any order provided we name the argument.

Types of Functions in Python : There are two types of functions in python :


 Functions returning some value /non – void functions/ Fruitful functions : The
returned value will internally substitute the function call statement.
Example :

NOTE : The return statement ends a function execution even if it is in the middle of the
function.
 Functions not returning any value / void functions / Non – fruitful functions :
These functions do not return any computed value.
It may or may not have a return statement, but if it has, then it takes the
following form :
return

Returning Multiple Values


Python allows us to return more than one value from a function.

Scope of Variables : It refers to the part of program within which a name is legal and
accessible. It is of two types :
 Global scope : A name declared in a top – level segment (__main__) of a program
is said to have a global scope.
 Local scope : A name declared in a function body is said to have a local scope.
Example :
 a1, b1 and result are Global variables
 a,b and c are local variables

Lifetime of a variable : The time for which a variable remains in memory is called lifetime
of a variable.
 For global variables, lifetime is entire program run.
 For local variables, lifetime is their function’s run.
Example :

 a1, b1 and result are Global variables


 a,b and c are Local variables

You might also like