Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

CH. 3 Working With Functions NOTES

Uploaded by

sharma20keshav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

CH. 3 Working With Functions NOTES

Uploaded by

sharma20keshav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Function Introduction

A Function can be defined as a named group of


instructions that perform a specific task and may return a
result. It only runs when it is called. We can pass data or
inputs, known as parameters, into a function.
Advantages of functions
 Program development made easy and fast: Work can be divided
among team members and completed in parallel making
development fast.

 Code re-usability : A function may be used later by many other


programs. This means that a python programmer can use
function written by others, instead of starting over from scratch.

 Reduces program size & Increases program readability: The


length of the source program can be reduced by using/ calling
functions at appropriate places so program become more
readable & organised.
Types of functions
1. Built-in functions: These functions are already built in
the python library and are always available for use. E.g.
input(), print(), int() etc.
2. Functions defined in modules: These functions are
defined in particular modules. When you want to use
these functions in program, you have to import the
corresponding module of that function. E.g. To work
with the functions of math module, we must import
math module in program.
3. User defined functions: The functions which are
defined by the user are called user defined functions
(UDF).
User Defined Functions
 To define a function keyword def is used
 After the keyword comes an identifier i.e.
name of the function, followed by
parenthesized list of parameters and the
colon which ends up the line.
 Next follows the block of statement(s) that
are the part of function.
Calling (using) the function
Once we have defined a function, we can call or use it from
another function, program or even the Python prompt. To
call a function we simply type the function name with
appropriate parameters*.
Syntax:
Function_name(parameter)

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.
Creating & calling a UDF
A function is defined using the def keyword in python.
e.g.
# Function block/ definition/creation
def func_MSG():
print("I am in function")

# _main_ program start here


print("I am in ", __name__)
func_MSG() # using/calling/invoking function
print("I am in main program again after calling function")
Note : The function definition should be made before the function call.
Parameters / Arguments Passing and
return value
Parameters or Arguments are specified after the function
name, inside the parentheses. Multiple parameters are
separated by comma. A functions has 2 types of
parameters:
 Formal Parameter: Formal parameters are written in the
function prototype and function header of the
definition. Formal parameters are local variables which
are assigned values from the arguments when the
function is called.
 Actual Parameter: When a function is called, the values
that are passed in the call are called arguments or
actual parameters. At the time of the call each actual
parameter is assigned to the corresponding formal
parameter in the function definition.
The return statement
The return statement is used to exit a function and go back to
the place from where it was called. There are 2 ways to return
values from a function.
Example-1: Function returning one value
def my_function(x):
return 5 * x
Example-2 Function returning multiple values:
def sum(a,b,c):
return a+5, b+4, c+7
S=sum(2,3,4) # S will store the returned values as a tuple
print(S) # or a,b,c =sum(2,3,4)
Parameters / Arguments Passing and
return value
The following example has a function with two parameters x
and y. When the function is called, we pass two values, which
is used inside the function to sum up the values and store in
z and then return the result(z):
def sum(x,y): # function definition with x and y as formal parameters
z=x+y
return z # return the result
a=float(input("Enter first number: " ))
b=float(input("Enter second number: " ))
r=sum(a,b) # Calling the function by passing actual parameters a and b
print(“Sum of 2 numbers is =“, r)
Types of Formal Arguments
Functions can be called using following types of formal
arguments −
 Positional arguments/Required arguments:
Arguments passed in correct positional order
 Default arguments -that assumes a default value if a
value is not provided to argument.
Positional Arguments
Positional arguments/ #Required arguments
Required arguments: These def square(x):
arguments should be z=x*x
passed in correct positional return z
order means we need to #main
match the number of r=square()
arguments with no. of print(r)
parameters required along # In above function square()
with the correct order. we have to definitely need to
pass some value to argument
x.
Default Arguments
Default arguments – #Default arguments
It allows us to assign def sum(x=3,y=4):
default values to a z=x+y
function arguments return z
which is useful in case #main
r=sum()
a matching argument print(r)
is not passed in the r=sum(x=10)
function call print(r)
statement, then the r=sum(x=10,y=20)
default value is used print(r)
in the function #default value of x and y is being used
when required arguments are not
definition. passed.
Scope of variables
The part of the program where a variable can be used is
known as Scope of variable.

 Two types of scopes :

● Global Scope

● Local Scope
Local Scope
With local scope, variable can be used only within the function / block that
it is created. Local variable is defined inside an function.
Eg:
 def test ( ):
 y = 20 # local variable
 print("inside test( ) value of x is ", x, " y is " , y)

# _main_ program start here


 x=50
 test()
 print("In main value of x is ", x, " y is " , y)

On executing the code we will get


inside test( ) value of x is 50 y is 20
The next print statement will produce an error, because the variable
‘y’ is not accessible outside the def()
Global Scope
 With global scope, variable can be used anywhere in the
program. A variable that is defined outside any function or
any block is known as a global variable.

eg:
def test ( ):
print("inside test x is ", x)

# _main_ program start here


x=50 # global variable
print("value of x is ", x)
test()
Output:
inside test x is 50
value of x is 50
Global Scope cont..
 To access global variable inside the function prefix keyword
global with the variable (spc. If u hv local & global with same
name)
 Eg:
 def test ( ):
 global x # accessing global variable in test()
 x=5
 y = 20
 print("inside function value of x is ", x, " y is " , y)

 # _main_ program start here


 x=50
 test()
 print("In main value of x is ", x)
 Output:
inside function value of x is 5 y is 20
In main value of x is 5

You might also like