0% found this document useful (0 votes)
7 views34 pages

Unit 1 Functions

The document provides an overview of functions in Python, including their definitions, types, and how to create and call them. It explains concepts such as parameters, return values, and variable scope, along with examples of function usage. Additionally, it outlines homework exercises and rules for combining arguments in function calls.

Uploaded by

ahishmourya16
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)
7 views34 pages

Unit 1 Functions

The document provides an overview of functions in Python, including their definitions, types, and how to create and call them. It explains concepts such as parameters, return values, and variable scope, along with examples of function usage. Additionally, it outlines homework exercises and rules for combining arguments in function calls.

Uploaded by

ahishmourya16
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/ 34

Unit-1

Working with Functions


Functions
• A Function is a sub program that acts on data
and often returns some value.
• A Function is a piece of code which performs
a task.
Understanding Functions
• def means a function definition is starting.
• Identifier following def is the function name.
• Variables inside parentheses are called as
arguments(parameters).
• Colon at end represents needed indentation.
• A function may/may not have a return
statement.
Sample Function
def aoc(r):
a=3.14*r*r
return a

def coc(rad):
circum=2 * 3.14 * rad
return circum

k=(int)(input("Enter Radius : "))


y=aoc(k)
print("Area of Circle with radius ",k," is ",y)
z=coc(k)
print("Circumference of circle with radius ",k," is ",z)
def aoc(r):
a=3.14*r*r
return a
k=int(input("Enter Radius : "))
y=aoc(k)

Example Python Function


Calling/Invoking Function
<function-name>(<values/variable passed)
Ex:-
aoc(4)

x=8
aoc(x)

k=(int)(input("Enter Radius : "))


aoc(k)

print(aoc(3))

Z=2 * aoc(6)
Home work
• Write a function to find a number is even or
odd.
• Write a function to take a number as input
and display a pattern
1
12
123
1234
12345
Python Function Types
1. Built in : pre defined functions
created by python and are always
available. (print(),input(),......)
2. Functions defined in Modules:
(math,random,pandas,....)
pow(),sqrt(),sin(),ciel(),randint(),se
ries(),........
1. Import module
2. Use them
3. User defined functions: functions
defined by us to meet new
requirements.
Defining Python Function
def <fun-name>(parameters):
<statements>
<statements>
def sum(x,y): def greet():
z=x+y print(“good morning”)
return z
Function Header: begins with def keyword and
ends with :
Parameters: variables listed in parentheses ()
Function body: block of statements indented,
may/may not have a return statement.
Even a function not returning any value can
have a return statement.
Structure of Python Program
def function1()
:
def function2()
:
#top level statements
Statement1
Statement2

Note: By default python names the top level segment as


__main__ and stores in a built in variable __name__
→Python always start execution from __main__ segment.
Flow of Execution
• Flow of execution refers to the order in which
statements are executed during a program
run.
• Function call will send the control flow to the
function definition.
• Last statement in the function definition will
send the control back to section from where
the function was called.
Flow of execution
Note:
• Program execution begins with first
statement of __main__ segment.
• def statements are also read but
ignored until called.
Types of Parameters
1. Positional/Required arguments
The function call statement must match the
number and order of arguments as defined in
function definition, these are called as
positional arguments.
Function Definition
def sim_int(p,t,r):
-----
-----
Function call
sim_int(a,b,c) sim_int(10000,b,c)

sim_int(10000,2,c)
2. Default arguments
A Parameter having default value in function
header is known as default arguments.
A parameter having a default value in function
header becomes optional(may or may not) in
function call.
Positional parameters should be before default
parameters.
Function Definition
def sim_int(p,t,r=2):
-----
-----
Function call
sim_int(10000,3)
sim_int(10000,3,4)
3. Keyword arguments
Keyword arguments are the named arguments
with assigned values being passed in the
function call statement.
Function Definition
def sim_int(p,t,r):
-----
-----
Function call
sim_int(p=30000,t=3,r=2)

sim_int(t=3,r=3,p=10000)

sim_int(r=2,t=2,p=15000)
Rules for combining arguments
→ An argument list must first contain
positional(required) params

→having positional args after keyword args will


result into error.

→having positional args after default args will


result into error.

→all required parameters are compulsory.


Excercise
def sim_int(prin, cc, t=3,r=2):
return (prin*t*r)/100
Function call Valid/Invalid

sim_int(prin=10000,cc=3) Valid

sim_int(r=3,prin=50000,cc=3) Valid

sim_int(15000,4,r=4) Valid

sim_int(r=5,20000,3) Invalid (pos. Args are after


keyword args)
sim_int(15000,prin=10000,cc=3) Invalid (Multiple values for prin)

sim_int(p=10000,cc=3) Invalid (p is not defined)

sim_int(10000,r=3,t=4) Invalid (No value for CC)


Returning Values
1. Non-void functions
→ Function returning some value
return value/variables/expressions
Ex: def sum(a,b):
→ return 10
→ return x
→ return x+y
→ return (a+b+c)//3
Function call
→ z=sum(10,20) → a=(b+c+d)/sum(2,20)
→ print(sum(240,320))
if sum(10,25) > 30:
print(“good”)
else:
print(“bad”)
Returning Values
2. void functions
→Function does not return any
value.(Even It may have a return
statement)
→Ex:- return

→ Function call
sum(10,20)
Parameters
3. With Parameters
if a function receives args then the
function can be treated a function with
parameters.

Ex: def sum(a,b):


-----
def greet(name):
------
Parameters
4. With out Parameters
if a function does not receives any args
then the function can be treated a
function with out parameters.

Ex: def sum():


a=10
b=20
def greet():
name=input(“enter a name”)
Returning value(s)
1. The return statement inside a
function body takes a below form:

return val1/exp1,val2/exp2,......
Returning value(s)
2. The function call statement
should receive the returned values
in the below form:
a)
K=tripple(x,y,z) #k will be a tuple

b)
a,b,c=tripple(x,y,z)
Arbitrary Arguments, *args
• If you dont know how many arguments that
will be passed to a function.
• We can handle with Arbitrary arguments.
• This parameter handles with *param and
processes as a tuple
Scope of Variables
• Parts of program within which a
name(variables) are legal and
accessible is called as scope of a
name.

1. Global Scope
A name declared in top level segment
(__main__) of a program is said to
have Global scope and is usable/
accessible to all parts (functions,
blocks) of a program.
Scope of Variables
2. Local Scope
A name declared in a function is said
to have Local scope, means it is
usable/accessible only in that
function.
Life time of a Variable
→The Time for which a variable
name remains in memory is called
lifetime of variable
→ For Global variables life time is
entire program (as long as
program is running)
→ For Local varibales life time is as
long as the control is in the
block/function.
• py
What if variable with same
name in global and local scope
?
Local Scope:
→Python will use the variable which
is defined in local scope.
Global scope:
→ Python will use the variable which
is defined in global scope.(local
scope variables cannot be used
here)
What if we want to use global
variable inside local scope
→We can use global statement to
indicate variables used are global
scope.

Example:

Global x
• py

You might also like