Function
Function
The positional parameters must occur to the left of default parameters because in a function
header, any parameter cannot have a default value unless all parameters appearing on its
right have their default values.
A global variable is a variable declared in top level segment (__main__) of a program and
usable inside the whole program and all blocks contained within the program.
A local variable is a variable declared in a function-body and it can be used only within
this function and the other blocks contained under it.
A default argument is an argument with a default value in the function header, making it
optional in the function call. The function call may or may not have a value for it.
3
greet("Alice")
greet("Bob", "Hi there")
Output
Hello Alice
Hi there Bob
In this example, the message parameter has a default value of "Hello". If no message
argument is provided during the function call, the default value is used.
Keyword arguments — Keyword arguments allow us to specify arguments by their parameter
names during a function call, irrespective of their position.
def person(name, age, city):
print(name, "is", age, "years old and lives in", city)
They are also called as non- They are also called as void
void functions. functions.
These variables have local scope. These variables have global scope.
Question 1(b)
What are the errors in following codes ? Correct the code and predict output :
def Tot(Number) #Method to find Total
Sum = 0
for C in Range (1, Number + 1):
Sum += C
RETURN Sum
print (Tot[3]) #Function Calls
print (Tot[6])
Answer
def Tot(Number): #Method to find Total
Sum = 0
for C in range(1, Number + 1):
Sum += C
return Sum
print(Tot(3)) #Function Calls
print(Tot(6))
Output
6
21
Draw the entire environment, including all user-defined variables at the time line 10 is
being executed.
1. def sum(a, b, c, d):
2. result = 0
3. result = result + a + b + c + d
4. return result
5.
6. def length():
7. return 4
8.
9. def mean(a, b, c, d):
10. return float(sum(a, b, c, d))/length()
11.
12. print(sum(a, b, c, d), length(), mean(a, b, c, d))
Answer
13
Find and write the output of the following python code :
a = 10
def call():
global a
a = 15
b = 20
print(a)
call()
Answer
Output
15
In the following code, which variables are in the same scope ?
def func1():
a = 1
b = 2
def func2():
c = 3
d = 4
e = 5
Answer
In the code, variables a and b are in the same scope because they are defined within the
same function func1(). Similarly, variables c and d are in the same scope because they are
defined within the same function func2(). e being a global variable is not in the same
scope.
Write a program with a function that takes an integer and prints the number that follows
after it. Call the function with these arguments :
4, 6, 8, 2 + 1, 4 - 3 * 2, -3 -2
Answer
1. def print_number(number):
2. next_number = number + 1
3. print("The number following", number, "is", next_number)
4. print_number(4)
5. print_number(6)
6. print_number(8)
7. print_number(2 + 1)
8. print_number(4 - 3 * 2)
9. print_number(- 3 - 2)
Output
The print_number following 4 is 5
The print_number following 6 is 7
The print_number following 8 is 9
The print_number following 3 is 4
The print_number following -2 is -1
The print_number following -5 is -4
Write a program with non-void version of above function and then write flow of execution
for both the programs.
14
Answer
The non-void version of above code is as shown below :
1. def print_number(number):
2. next_number = number + 1
3. return next_number
4. print(print_number(4))
5. print(print_number(6))
6. print(print_number(8))
7. print(print_number(2 + 1))
8. print(print_number(4 - 3 * 2))
9. print(print_number(-3 - 2))
Output
5
7
9
4
-1
-4
What will be the output of the following Python code ?
V = 25
def Fun(Ch):
V = 50
print(V, end = Ch)
V *= 2
print(V, end = Ch)
print(V, end = "*")
Fun("!")
print(V)
1. 25*50!100!25
2. 50*100!100!100
3. 25*50!100!100
4. Error
Answer
25*50!100!25
15
default_volume = calculate_volume()
print("Volume of the box with default values:", default_volume)
v = calculate_volume(10, 7, 15)
print("Volume of the box with default values:", v)
b = calculate_volume(width = 19)
print("Volume of the box with default values:", b)
Output
Volume of the box with default values: 30
Volume of the box with default values: 1050
Volume of the box with default values: 414
Volume of the box with default values: 190
Question 3
Write a program to have following functions :
(i) a function that takes a number as argument and calculates
cube for it. The function does not return a value. If there
is no value passed to the function in function call, the
function should calculate cube of 2.
(ii) a function that takes two char arguments and returns
True if both the arguments are equal otherwise False.
Test both these functions by giving appropriate function call
statements.
Solution
# Function to calculate cube of a number
def calculate_cube(number = 2):
cube = number ** 3
print("Cube of", number, "is", cube)
calculate_cube(3)
calculate_cube()
char1 = 'a'
char2 = 'b'
print("Characters are equal:", check_equal_chars(char1, char1))
print("Characters are equal:", check_equal_chars(char1, char2))
Output
Cube of 3 is 27
Cube of 2 is 8
Characters are equal: True
Characters are equal: False
Question 4
Write a function that receives two numbers and generates a
random number from that range. Using this function, the main
program should be able to print three numbers randomly.
Solution
import random
for i in range(3):
random_num = generate_random_number(num1, num2)
print("Random number between", num1, "and", num2, ":", random_num)
Output
Enter the first number: 2
Enter the second number: 78
Random number between 2 and 78 : 77
Random number between 2 and 78 : 43
Random number between 2 and 78 : 52
s1 = "hello"
s2 = "world"
s3 = "python"
print(same_length_strings(s1, s2))
print(same_length_strings(s1, s3))
Output
True
False
Question 6
Write a function namely nthRoot( ) that receives two
parameters x and n and returns nth root of x i.e., x^(1/n).
The default value of n is 2.
Solution
def nthRoot(x, n = 2):
return x ** (1/n)
result = nthRoot(x, n)
print("The", n, "th root of", x, "is:", result)
default_result = nthRoot(x)
print("The square root of", x, "is:", default_result)
Output
Enter the value of x:36
Enter the value of n:6
The 6 th root of 36 is: 1.8171205928321397
The square root of 36 is: 6.0
Question 7
Write a function that takes a number n and then returns a
randomly generated number having exactly n digits (not
starting with zero) e.g., if n is 2 then function can randomly
19
return a number 10-99 but 07, 02 etc. are not valid two digit
numbers.
Solution
import random
def generate_number(n):
lower_bound = 10 ** (n - 1)
upper_bound = (10 ** n) - 1
return random.randint(lower_bound, upper_bound)