Function 12
Function 12
A function is a group of statements that perform a specific task. A function executes when it is called.
Advantages of Functions in Python
1. Program development made easy and fast.
2. Program testing becomes easy.
3. Re-usability of code increases.
4. Reduced length of program.
Built in Functions :
Those functions which are already available in python is called built-in functions. These are always available in
the standard library. We don’t have to import any modules to use such built-in functions. Let we discuss bult-in
functions in python
1. int()
This function converts any value(string or float) into an integer value. for example
>>>int('123')
123
>>>int(23.34)
23
>>>int(-56.23)
-56
>>>int("CSIP")
above statement returns Value Error
>>>int()
0
NOTE: This function converts the floating point number to integer without rounding off. It simply removes the
decimal part.
2. float()
This function converts integers into floating point numbers. for example
>>>float(21)
21.0
>>>float("12.325")
12.325
>>>float("CSIP")
above statement return Value Error
>>>float(148.23)
148.23
>>>float()
0.0
3. input()
This function help to accept any value from the user through keyboard. This function always return a string
value which can be converted into integer using int( ) functions. for example
n1 = input("Enter any number : ")
print(n1)
OUTPUT:
Enter any number : 9
9
--------------------------------------------------------------
n1 = input("Enter any number : ")
print(n1 + 3)
OUTPUT:
Enter any number : 5
TypeError # as input function returns a string.
-------------------------------------------------------------
n1 = int(input("Enter any number : "))#using int() function to convert string to integer
print(n1 + 3)
OUTPUT:
Enter any number : 8
11
4. eval()
This function takes the string value and evaluates. If the argument is not string then eval( ) function returns an
error. for example
>>>eval('2' + '6')
26
>>>eval('3' + '5')
8
>>>eval(3 + 8)
Type Error
5. max()
This function returns the largest value out of the given arguments. for example
>>>max(6, 9, 12, 32)
32
>>>max('d', 5, 7.8, 9)
Type Error #String and Integer can not be compared
>>>max('a', 'b', 'f', 'j') #it returns the largest value according to ASCII value
'j '
6. min()
This function returns the smallest value out of the given arguments. for example
>>>min(1, 2, 3, 4)
1
>>>min(10.5, 2.9, 3, 4)
10.5
>>>min('a', 'b', 'f', 'j') #it returns the smallest value according to ASCII value
'a'
7. abs()
This function always return an absolute value(positive value) of a number. for example
>>>abs(34)
34
>>>abs(-56.7)
56.7
>>>abs( )
TypeError: abs() takes exactly one argument (0 given)
>>>abs(78-100)
22
8. type()
This function returns the data type of a variable or a value. In other words we can say that it simply returns the
type of value hold by a variable. for example
>>>type(25)
<class 'int'>
>>>x =7.6
>>>type(x)
<class 'float'>
>>>type('True')
<class 'str'>
>>>type(True)
<class 'bool'>
9. len()
This function return the total number of items in an object. The object may be String, tuple, list etc. for example
>>>len((1, 3, 45, 32, 'a'))
5
10. range()
This function generates a sequence of number. This function is mainly used with ‘for’ loop. for example
>>>range(4)
range(0, 4)
The above function generates four values ie (1, 3, 5, 7). To see the output of above function, execute the
following statement.
>>>list(range(1, 9, 2))
[1, 3, 5, 7]
User Defined Functions in Python
A set of statement that performs a specific task is called function. A function which is defined or created by the
user is called user defined function.
A user defined function is created by the def keyword followed by function name, parenthesis and colon.
Practical Task
OUTPUT:
Hello World
NOTE: The above function is not returning any value. Such functions are called void functions
Task 2: Create a function which will print “Computer Science” and Informatics Practices in different lines.
def display( ):
print("Computer Science")
print("Informatics Practices")
OUTPUT:
Computer Science
Informatics Practices
Return statement specifies what value to be returned to the calling function. Function takes input through
parameters and return the output after processing.
Task 3: Create a function which will take side of a square as parameter and return the area.
def area(side):
ar = side * side
return(ar) #After calculation, it is returning the value of area to the calling function
a=area(7)
print("Area of a Square is : ", a)
OUTPUT:
Area of a Square is : 49
Task 4: Write a function which will take two numbers as parameter and return the added value.
def add(n1, n2):
sum = n1 + n2
return(sum)
a=add(7, 9)
print("Sum of the numbers are : ", a)
OUTPUT:
Sum of the numbers are : 16
Task 5: Write a function which will take two numbers as parameter and return the addition, difference, product
and division.
def math_operations(n1, n2):
sum = n1 + n2
diff = n1 - n2
mul = n1*n2
div = n1/n2
return(sum,diff,mul,div) #A function can return multiple values
a=math_operations(9, 3)
print("Result of operations are : ", a)
#we can also display result using loop
for i in a:
print(i)
OUTPUT:
Result of operations are : (12, 6, 27, 3.0)
12
6
27
3.0
Parameters are the variables which we write in the parenthesis after the function name in the function header.
for example
def calculate(x, y) #Here x and y are called Formal Parameters
An argument is a value which we passed to a function through function calling statement. Such arguments are
called Actual Parameters or Actual Arguments.
OUTPUT:
p is 5 and q is 8 and r is 5
p is 15 and q is 15 and r is 24
p is 50 and q is 15 and r is 40
NOTE: If we are not passing any value to an argument, then default value will be assigned. If we are passing a
value then this value over write the default value.
3. Keyword arguments
In a function call, we can specify the values of arguments using their names instead of their position or order.
These are called Keyword or Name arguments.
def message(rno, name, clas):
print("Your Roll Number is : ", rno)
print("Your Name is : ", name)
print("Your Class is : ", clas)
return a,b
S=act(3)
print(S)
OUTPUT
(5, 15)