Advertisement
# Python - Defining functions with formal arguments
# Defining a function named square with a formal parameter named num
def square(num):
result = num*num;
return result;
a = 5
# Square() function is called
result = square(a)
print('Square of a is = ', result);
Square of a is = 25
# Defining a function named square with a formal parameter named num
def square(num):
result = num*num;
return result;
# Square() function is called
result = square(a)
Advertisement
# Python - Multiple functions of actual arguments with different type
# square function is defined
def square(num) :
square_result = num*num
return square_result
a =10
f = 10.5
# square() function is called
print('Square of 10 is = ', square(10))
#square() function is called again with a floating-point value
print('Square of 10.5 is = ', square(10.5))
Square of 10 is = 100
Square of 10.5 is = 110.256
# Python - Calling a function with actual argument of an incompatible type
# square function is defined
def square(num) :
square_result = num*num
return square_result
# square() function is called with string value, resulting in a compile error
print('Square of abc is = ', square('abc'))
Square of 10 is = 100
Square of 10.5 is = 110.25
Traceback (most recent call last):
File "C:/Users/RadheKrishna/Desktop/Python Programs/func6.py", line 27, in
print('Square of abc is = ', square('abc'))
File "C:/Users/RadheKrishna/Desktop/Python Programs/func6.py", line 7, in square
square_result = num*num
TypeError: can't multiply sequence by non-int of type 'str'
# Python - Defining functions with multiple arguments
# addition function is defined
def addition(a,b):
add = a+b
return add
# subtraction function is defined
def subtraction(a,b):
subtract = a-b
return subtract
a,b = 10,5
# addition() function is called
result = addition(a,b)
print('Result of 10 + 5 = ', result)
# subtraction() function is called
result = subtraction(a,b)
print('Result of 10 - 5 = ', result)
Result of 10 + 5 = 15
Result of 10 + 5 = 5
# Python - Defining function with mixed arguments types
# Defining a function named misc with mixed arguments
def misc(intg, st, fl):
print('integer value : ', intg)
print('character value : ', st)
print('floating-point value : ', fl)
# A variable to contain an integer value
i = 5
# A variable to contain a string value
string = 'hello'
# A variable to contain a floating-point value
f = 9.9
# misc() function is called
misc(i,string,f)
integer value : 5
character value : hello
floating-point value : 9.9
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement