0% found this document useful (0 votes)
4 views

Functions

Uploaded by

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

Functions

Uploaded by

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

Functions in python

agenda
Why to use function?
Function definition
Function call
example
Why to use function?
Easier to read and debugging
Reusability
Dividing long program into functions
What are functions?
Syntax :
def function_name(parameters):
statement(s)
Functions in python
Function definition:
o every function should start with “def”
keyword
o Name of the function
o Parameters/arguments(optional)
o Function name should end with :
o Return (empty or value)
o Multi value return can done(using tuples)
Function call
Function name
Arguments and parameters
Example for function
#example for function in python

# function definition
def add(num1, num2):
sum=num1+num2
return sum

#actual code

num1=int(input("enter the first number:\n"))


num2=int(input("enter the second number:\n"))
# function call
res=add(num1,num2)
print("Result=",res)
Output:
enter the first number: 10
enter the second number: 20
Result= 30
Example:2 swapping of 2 numbers
 # swapping of 2 numbers

def swap(num1,num2):
temp=num1
num1=num2
num2=temp
print("after swapping num1 and num2is:",num1,num2)

#actual code

num1=int(input("enter the number1:"))


num2=int(input("enter the number2:)"))
print("before swapping num1 and num2 is:",num1,num2)
res=swap(num1,num2)
Output:
enter the number1:10
enter the number2:20
before swapping num1 and num2 is: 10 20
after swapping num1 and num2is: 20 10
Local variable and global variable
A variable is a label or a name given to a
certain location in memory.
This location holds the value you want your
program to remember, for use later on.
Variable Scope:
The part of a program where a variable is
accessible is called its scope
Local Scope

Whenever you define a variable within a


function, its scope lies ONLY within the
function.
Global scope
Whenever a variable is defined outside any
function, it becomes a global variable, and its
scope is anywhere within the program.
Which means it can be used by any function.
Conti.
Fruitful functions:
a functions returns a value.
Void functions:
a function that always returns None.
Types of arguments in function
Required arguments
Keyword arguments
Default arguments
Variable length arguments
Required arguments
Same and order
Example:
#example for required arguments
def display(num1,num2):
print(num1,num2)

display(100,200)
Keyword arguments
Oder or position is not required
Initialization done base of keywords
Example:
#expample for keyword arguement

def display(num1,num2):
print(num1,num2)

display(num2=10, num1=20)
Default arguments
Number of argument need not be same
Some of arugements will be consider as default
Example:
#example for default arguements

def display(name, course="MCA"):


print(name)
print(course)

display(name="robert",course="BCA")
display(name="rishab")
Variable length arguments
Arbitrary number of arguments
By placing * as prefix to the arguments of function
definition:
Example:
#example for variable arguements
def display(*course):
for i in course:
print(i)

display("MCA","BCA","M.Tech","B.E")

You might also like