Working With Functions
Working With Functions
•
Understanding functions:
#simple Function
#Function definition
def calc(x): # Function Header
res = 2 * x**2 # Function body
return res
#main program: #Main program
n = int(input("Enter a number : "))
ans = calc(n) # Function call statement/ or invoke function
print("Answer = : ", ans)
Python function types:
• Built-in functions :
• These are predefined functions and always available for use.
For e.g. input(), len(), print(), etc.
• Functions defined in modules :
• These functions are predefined in particular modules and can only
be used when the corresponding module is imported.
For e.g. if you want to use function sin(), you need to import the module math
(that contains definition of the function sin()).
• User Defined Functions:
• These functions are defined as per user requirements.
For e.g. to find the sum of two numbers.
Calling/ Invoking/ Using functions:
• To use the functions, you need to write a function call statement.
For e.g.
test(10, 17) then a will have value 10, b will have 17 and c will have value 5
test(10, 17,100) then a will have value 10, b will have 17 and c will have value 100
3. Keyword (Named) arguments
• To have complete control and flexibility over the values sent
as arguments for the corresponding parameters, Python
offers another type of argument Keyword (Named)
arguments.
• Keyword arguments are the named arguments with
assigned values being passed in the function call statement.
• You can write any argument in function call statement in any
order provided you name the arguments when calling the
function.
Keyword (Named) arguments
The output :
For eg.
Output -1 test(x, y, z)
#Keyword arguemnts
a = 10
def test(a, b, c):
b = 20
print("a = ",a)
c = 30
print("b = ",b) Output -2 test(c = 7, a = 14, b = 45)
print("c = ",c) a = 14
x = 10 b = 45
y = 20 c= 7
z = 30 Output -3 test(c = y, a = z, b = x)
print ("Output -1 ", "test(x, y, z)") a = 30
test(x, y, z) b = 10
print ("Output -2 ", "test(c = 7, a = 14, b = 45)") c = 20
test(c = 7, a = 14, b = 45)
print ("Output -3 ", "test(c = y, a = z, b = x)")
test(c = y, a = z, b = x)
Using Multiple types of arguments:
• Python allows you to combine multiple types of
arguments:
• Rules for combining all types of arguments:
• An argument list must first contain positional arguments
followed by any keyword argument.
• Keyword arguments should be taken from the required
arguments preferably.
• You cannot specify a value for an argument more than
once.
Returning values:
• The function call statement should receive or use the returned values
in one of the following ways:
• Either receive the returned values in form of tuple variable.
Returning values:
• Returning multiple values:
def squared(x,y,z):
return x*x, y*y, z*z
t = squared(2, 3, 5)
print(t) # output will be printed as : (4, 9, 25)
You can directly unpack received values of tuple by specifying the same
number of variables on the left-hand side of the assignment in function
call, e.g.
def squared(x,y,z):
return x*x, y*y, z*z
a, b, c = squared(2, 3, 5)
print(a, b, c) # output will be printed as : 4, 9, 25
Composition:
• Using an expression as a part of larger expression.
Scope of variables:
• Scope of a variable means part(s) of a program in which
variable is accessible.
For e.g.
def test_fun():
x = 10
print(“value of x inside function : ", x)
Here, x is a local variable.
Scope of variables:
Global variable :
variable which is accessible among whole program using global
keyword.
Non local variable –accessible in nesting of functions, using nonlocal
keyword.
x=5
def test_fun():
global x
x = 10
x=x+1
print("Global value inside function : ", x)
x = x**2
print("Value before function call : ",x)
test_fun()
print("Value after function call : ",x)
Functions using libraries:
• Mathematical, and string functions.
• Mathematical functions:
• Mathematical functions are available under math module. To use
mathematical functions under this module, we have to import the
module using the command :
import math
For e.g.
To use sqrt() function we have to write statements like given below.
import math
x = math.sqrt(4)
print(x)
output will be 2
Functions using libraries:
String functions:
• String functions are available in python standard module.
These are always available to use.
• For e.g. capitalize() function converts the first character of
string to upper case.