Run Your Python Programs Online
Computer Science
( Class XII Sc )
( Subject Code 083)
Learn the concept of function in python and attempt the quiz
CLICK HERE FOR QUIZ
Function
1. User Defined : defined by the user using def keyword
2. built-in functions: Predefined function in python language
# This function returns a tuple
# sum of n mumbers using variable length arguments with function
def sum(*n):
m=n[0]
for i in n:
s=s+i
print(s)
sum(2,5,6)
sum(12,15,6)
sum(22,5,6,2.5)
#Find largest number among given numbers
def maximum(*n):
m=0
for i in n:
if(i>m):
m=i
return m
print(maximum(1,2,3,4)) # 4
print(maximum(11,22,3,934,66,77,88,555)) #555
print(maximum(111,2,3,4,66,7,8,555,33)) #555
Positional Arguments
We cannot change the position of arguments.
if change the output will be changed.
def sub(a,b):
print(a-b)
sum(20,35) # 20 will be assigned in a and 35 will be assigned in b
sum(35,20) # 35 will be assigned in a and 20 will be assigned in b
Keyword Arguments
When we Call above function with their arguments name . Then we need not to remember the position of arguments
ef sub(a,b):
print(a-b)
sum(a=20,b=35) # 20 will be assigned in a and 35 will be assigned in b
sum(n=35, a=20) # 35 will be assigned in a and 20 will be assigned in b
output
-15
-15
CLICK HERE FOR QUIZ
Learn the concept of function in python and attempt the quiz
CLICK HERE FOR QUIZ
Function
- Function is a block of code that perform a particular task
- It only runs when it is invoked or called.
- You can pass data, known as parameters, into a function.
- A function can return data as a result using return keyword.
Types of functions
1. User Defined : defined by the user using def keyword
2. built-in functions: Predefined function in python language
Creating/Defining Function:
def my_function():
print("Hello from a function")
print("Hello from a function")
Calling Function
my_function()
Parameters(arguments): information
passed in a function during creation or calling is called parameters.
def sum(a,b):
s=a+b
print(s)
x=10
y=20
sum(x,y)
# a , b and x,y
are called parameters.
Actual parameters: parameters
passed in function calling. In above code x and y are actual parameters
Formal parameters: parameters
passed in function definition. In above code a and b are formal parameters
Function return value using return keyword
def sum(a,b):
s=a+b
return s
x=10
y=20
n=(x,y)
print(n)
A function can return multiple values
# This function returns a tuple
def fun():
str = "ajay"
x = 20
return str, x; # Return tuple, we could also write (str, x)
str, x = fun() # Assign returned tuple
print(str)
print(x)
A function can return multiple values using list or tuple or dictionary
# This function returns a tuple
def
fun():
str =
"mukesh"
x
= 20
return
str,
x; # Return tuple, we
could also write (str, x)
t =
fun() # Assign returned tuple
print(t)
output:
('mukesh', 20)
# This
function returns a dictionary
def
fun():
d
= dict();
d['str'] = "abc"
d['x']
= 20
return
d
#
Driver code to test above method
d =
fun()
print(d)
# This
function returns a list
def
fun():
str = “abc"
x
= 20
return
[str,
x];
#
Driver code to test above method
list
= fun()
print(list)
Variable length argument:
We can
pass any number of arguments in a functon using
* symbol.
# sum of n mumbers using variable length arguments with function
def sum(*n):
m=n[0]
for i in n:
s=s+i
print(s)
sum(2,5,6)
sum(12,15,6)
sum(22,5,6,2.5)
#Find largest number among given numbers
def maximum(*n):
m=0
for i in n:
if(i>m):
m=i
return m
print(maximum(1,2,3,4)) # 4
print(maximum(11,22,3,934,66,77,88,555)) #555
print(maximum(111,2,3,4,66,7,8,555,33)) #555
Function with Default Arguments
Default
Arguments: Function
arguments can have default values in Python. We can provide a default value to
an argument by using the assignment operator (=). Here is an example.
If we don’t pass any value to the function then it will take a pre defined value.
If we don’t pass any value to the function then it will take a pre defined value.
def hello(name, msg =
"Good morning!"):
print("Hello",name + ',
' +msg)
hello("mukesh")
hello("Modi","How do
you do?")
Note
->Any number of arguments in a function can
have a default value.
->non-default
arguments cannot follow default arguments.
For example, if we had defined the
function header above
def hello(msg =
"Good morning!", name):
SyntaxError:
non-default argument follows default argument
Positional Arguments
We cannot change the position of arguments.
if change the output will be changed.
def sub(a,b):
print(a-b)
sum(20,35) # 20 will be assigned in a and 35 will be assigned in b
sum(35,20) # 35 will be assigned in a and 20 will be assigned in b
Keyword Arguments
When we Call above function with their arguments name . Then we need not to remember the position of arguments
ef sub(a,b):
print(a-b)
sum(a=20,b=35) # 20 will be assigned in a and 35 will be assigned in b
sum(n=35, a=20) # 35 will be assigned in a and 20 will be assigned in b
output
-15
-15
CLICK HERE FOR QUIZ
Comments
Post a Comment