[go: up one dir, main page]

0% found this document useful (0 votes)
8 views87 pages

Working With Functions PPT 1

Uploaded by

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

Working With Functions PPT 1

Uploaded by

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

WORKING WITH FUNCTIONS

INTRODUCTION
Function: - A function is a subprogram that act
on data and often return a value.
Python functions can belong to one of the
following three categories:
1. Built-in Function
2. Functions defined in modules
3. User defined functions
Reason of using FUNCTIONS
• A function is a named unit of a group of
program statements. This unit can be invoked
from other parts of a program.
• To make program handling easier, avoiding
ambiguity.
• To reduce the program size.
• To make program more readable and
understandable
EXAMPLE
• f(x): f can be termed as a function and x is its
argument.
• In a programming language, function can
have:
>arguments
>can perform certain functionality (some set of
stmts)
>can return a result
• Example:
f(x)=2x
CALLING/INVOKING /USING FUNCTION
• To use a function which has been already
defined, just write a function call statement.
• SYNTAX:
• EXAMPLE:
To call calcSomething(), the function call statement
will be:
#5 is being sent as an argument.

2)

** Number of values passed is same as number of


arguments.
PRACTISE QUESTIONS
• Write a function to calculate the cube and call
the function in main program.
Different ways
(i)Passing literal as argument in function call.
cube(4)
(ii)Passing variable as argument in function call.
num=4
cube(num)
(iii)Taking input and passing the input as argument in
function call
mynum=int(input(“enter the no.))
cube(mynum) #pass value as variable mynum to
argument
(iv)Using function call inside another statement.
print(cube(3))
(v)Using function call inside expression.
result=2*cube(6)
Types of Function
DEFINING FUNCTION
• A function once defined can be called as many
times as needed in the main program.
PARTS OF THE FUNCTION
• Function header: - The first line of the function definition
that beings with keyword def and ends with a colon (:),
specifies the name of the function and its parameters.
• Parameters: - Variables that are listed within the parentheses
of a function header.
• Function body: - The block of statement/indented -
statement beneath function header that defines the action
performed by the function. The function body may or may
not return any value.
• Indentation: - The blank space in the beginning of statement
within a block. All statements within same block have same
indentation.
Example
Some more…….
• A function definition does not execute the
function body; this gets executed only when
the function is called or invoked.
The flow of execution
The flow of execution refers to the order in
which statement are executed during a
program run.
• A function body is also a block, a block is
executed in an execution frame.
• WAP a program to multiply 2 numbers through user defined
function.
def mul(x,y):
m=x*y
return m
Num1=int(input(“enter the first number:”))
Num2=int(input(“enter the second number:”))
P=mul(Num1,Num2)
print(“product is:”,P)
Argument: - The values being passed through a function call statement are called
argument (or actual parameters or actual argument).
For example:-
def calcSum ( x , y ):
s=x+y
return s

print (calcSum ( 2 , 3 ))
a=5
b=6
print (calcSum ( a , b ))
d = 10
print (calcSum ( 9 , d ))
· Here a , b , d , 2 , 3 , 9 are “arguments” which is used in call function.
Parameters: - The values received in the
function definition header are called parameter
(or formal parameters or formal arguments).
For example: -
def calcSum ( x , y ):
:
Here x , y are “parameters”
Arguments in python can be one of these values:
>literals
>variables
>expressions
ARGUMENT: Actual parameter/actual argument
PARAMETER: formal parameter/formal argument
NOTE DOWN…..
IMPORTANT POINT……..
Example

Example: Arguments in function call


def SI (P,R,T):
>>> SI (1000,2,10)
1000,2,10 are arguments. An argument can be
constant, variable, or expression.
Write the output from the following function:
def SI(p,r=10,t=5):
return(p*r*t/100)
if we use following call statement:
SI(10000)
SI(20000,5)
SI(50000,7,3)
output
• Output
• >>> SI(10000)
• 5000
• >>> SI(20000,5)
• 5000
• >>> SI(50000,7,3)
• 10500
PASSING PARAMETERS
Function call must provide all the values as required
by function definition. If the function header has 3
parameters then the function call should also pass 3
values.
Python supports 3 types of formal
arguments/parameters:
1.Positional arguments(required arguments)
2. Default arguments
3.Keyword(or named) arguments
POSITIONAL/REQUIRED ARGUMENTS
• Match the number of arguments with number
of parameters required.
• Positional Argument Matching: when the
function call statement must match the
number and order of arguments as defined in
the function definition, this is called as
Positional Argument Matching.
DEFAULT ARGUMENTS
• Python allows to assign default value(s) to a
function’s parameter(s) which is useful in case
a matching argument is not passed in the
function call statement.
• The default values are defined in the function
header.
VALID OR INVALID????
KEYWORD(NAMED)ARGUMENTS
• You cannot change the order of the arguments
in the function call.
MULTIPLE ARGUMENT TYPES
VALID OR INVALID
PROGRAM#1
Q1: Write a program to calculate simple interest
using a function interest()that can receive
principal amount , time and rate and returns
calculated simple interest. Default values for
rate and time as 10% and 2 years respectively.
RETURNING VALUES FROM FUNCTIONS
NON-VOID FUNCTIONS
EXAMPLE
def sum(x,y):
s=x+y
return s
result=sum(5,3)
MAKE A NOTE…..
VOID FUNCTIONS
• Void function doesnot return any value but it
return an empty value called NONE.
PREDICT THE OUTPUT
RETURNING MULTIPLE VALUES
• Functions can return multiple values.

1)Either receive the returned values in the form


of tuple variable.
Ex:
def mul(x,y,z):
return x*x,y*y,z*z
T=mul(2,3,4)
print(t)
ANOTHER WAY
PROGRAM#2
WAP that receives 2 numbers in a function and
returns the results of all arithmetic
operations(+,-,*,/,%) on these numbers.
SCOPE OF VARIABLES
• The scope rules of a language are the rules
that decide, in which part(s) of the program , a
particular piece of code or data item would be
known and can be accessed therein.
REAL LIFE EXAMPLE
SCOPE OF VARIABLES
• Scope of name refers to part(s) of a program
within which a name is legal and accessible
TYPES:
1. GLOBAL
2. LOCAL
GLOBE SCOPE
• The name declared in the main program is
said to have a global scope, and is usable
inside the whole program and all blocks
(functions , other blocks) contained within the
program.
EXAMPLE:???(real life example)
LOCAL SCOPE
• A name declared in a function-body is said to
have local scope.
• It can be used only within the function and the
other blocks contained it . The names of
formal arguments also have local scope.
Example
GLOBAL ENVIORNMENT LOCAL(calcSum())
Num1>3 X=3
Num2>7 Y=7
Sum=10 Z variable is created in
local environment.
Z=10
Value of z will be returned
to call end of the function.
EXAMPLE-2
EXAMPLE-3
GLOBAL LOCAL
Num1,num2,num3 a,b,c>calcsum()
x,y,z > average()
CASE-1
CASE-2
CASE-3
GLOBAL VARIABLE INSIDE LOCAL SCOPE
 USE THE global statement.
COMPARISION

You might also like