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

Functions

The document discusses functions in programming. It defines what a function is, the different types of functions, and how to create user defined functions. It also covers function parameters, return values, scope, and some built-in functions.

Uploaded by

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

Functions

The document discusses functions in programming. It defines what a function is, the different types of functions, and how to create user defined functions. It also covers function parameters, return values, scope, and some built-in functions.

Uploaded by

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

FUNCTIONS

BY PRAJEESH M
FUNCTION
 ()
GROUP OF STATEMENTS (block)
 COMBINED TOGETHER WITH A NAME
DO A PARTICULAR TASK
POSITIVES
CODE REUSABILITY
USED TO REDUCE PGM SIZE
MAKE PGM MORE READABLE AND
UNDERSTANDABLE.
SAVES TIME
TYPES OF FUNCTIONS

USER
DEFINED

FUNCTIONS BUILD IN input().print()

FUNCTIONS
IN MODULES sqrt(),sin()
USER DEFINED FUNCTIONS
 CREATION OF A FUNCTION IS CALLED FUNCTION
DEFINTION
ACCESSING OF A FUNCTION IS CALLED FUNCTION
CALLING
CREATION OF USER DEFINED FUNCTIONS
• FUNCTION DEFINITION
• SYNTAX:

def functionname(parameters): Function header


statement(s) Function body
Indendation
NB Parameters are optional
PARAMETERS DATA INTO A FUNCTION

• Values/parameters
specified at the
Formal time of function
definition

Parameters

• Values/parameters
specified at the
Actual time of function
call
ACCESSING A FUNCTION
• FUNCTION CALLING
• SYNTAX:
• functionname(ACTUAL_parameters)

A function should define before its call


PROCESSINGFUNCTION BODY

Eg:
def fact(a):
f=1
for i in range(1,a+1):
f=f*i
print(f)
fact(5)

5actual parameter
a formal parameter
 return a
 return 'a'
 return a+b
 return (a+b)/10
 s=9*23
 return s

f's returning value----non void----fruitful functions


f's not returning value----None(void)
WAPP TO FIND SUM OF 2 NOS
a=int(input(“Enter value1:”))
INPUT
b=int(input(“Enter value2:”))
sum=a+b PROCESSING

print(“Sum is :”,sum) OUTPUT


 1. def sum():------------------>Function definition
a=int(input(“Enter value1:”))
b=int(input(“Enter value2:”))
print(a+b)
:
:
sum()------------------>Function calling

 2. def sum(a,b):------------------>Function definition


print(a+b)
:
:

c=int(input("Enter the value1:"))


d=int(input("Enter the value2:"))
sum(c,d)------------------>Function calling
 3. def sum():------------------>Function definition
a=int(input(“Enter value1:”))
b=int(input(“Enter value2:”))
return(a+b)
:
:
:
:
print(sum())------------------>Function calling
 4. def sum(a, b):------------------>Function definition
return(a+b)
:
:
:
c=int(input("Enter the value1:"))
d=int(input("Enter the value2:"))
 print(sum(c,d))------------------>Function calling
TYPES OF ARGUMENTS/PARAMETERS
 arguments--values being sent(at call)[Actual parameter]
 parameters---values being received(at defintion)[formal parameter]

 1.POSITIONAL/REQUIRED/MANDATORY
 def sum(a,b,c):
print(“Sum is”,a+b+c)

d,e,f=10,20,30
sum(d,e,f)
or
sum(10,20,30)
or
sum(10,20+30,d+e+f)
sum() ×
2.Default argument
def sum(a,b=100,c=200):
print(“Sum is”,a+b+c)

d,e,f=10,20,30
sum(d,e,f)60
sum(d,e) 230
sum(d)310
sum() ×
3.KEYWORD /NAMED ARGUMENTS
def sum(theory,practical)

sum(70,30)
sum(practical=30,theory=70)
4.Variable length argument
 def just(*h)
print(h)
just(1,2,3,45)
*data stored as tuples
Scope of variables
Parts of a program in which variables can be used
1. Local scope
* Variable that defined/declared inside a function or
block
* Can be used/ accessed only within that function
2. Global scope
* Outside all functions
* Can be used/ accessed anywhere in that program
Life time of a variable:
Time for which variable remains in the memory
 Global variables can be created before/after all
function definitions
Eg
a=10
def hello():
print(a)

hello()10
print(a)10
def hello():
x=“hello”
print(a)

a=10
hello()10
print(x)×
print(a)
Use of global keyword
def state1():
tigers=15
print(tigers)
tigers=95
print(tigers)
state1()
print(tigers)
---------------------------------------------------------
def state1():
global tigers
tigers=15
print(tigers)
tigers=95
print(tigers)
state1()
print(tigers)
Flow of execution
1. def hello():
2. print(“Hello”)
3. def hi():
4. print(“hi”)
5. hello()
6. hi()
7. print(“Bye”)
1-->3-->6-->3-->4-->5-->1-->2-->6-->7
Random module
 import random
 print(random.random())---->0-1(.23, .99)(including 0
excluding 1+takes no arg)
 print(random.randint(1,7))--->1 and 7 included {Accept 2
para}
 print(random.randrange(1,7))--->7 not included
 print(random.randrange(1,7,2))--->7 not included[1,3,5]
 print(random.choice('computer'))
 print(random.choice([2,56,45,90,34]))
 print(random.uniform(1,100)[1 inclu 100 exclu+float
values]
Eg:
Some useful inbuilt functions
 math module:
 import math
 print(math.sqrt(9))3.0
 print(math.pow(2,3))8.0
 print(math.floor(42.45))42 ,int
 print(math.ceil(42.4))43,int
 print(math.fabs(-9))9.0
 round(12.53)--int, 13
 round(12.43)--int, 12
 round(12.53,0)--float, 13.0
 round(12.43,0)--float, 12.0
 round(12.53,1)--float, 12.53
 round(12.4378,2)--float, 12.44

You might also like