CBSE Coding Class 7 Solution – Fun with Functions
CBSE Coding Class 7 Solution Student Handbook Chapter 3 Fun with Functions all Questions and Answers Solution by Coding Teacher. Net Ex. Here provided all Coding Solution for Class 7.
1.) Function in a program decreases readability
Answer: False
Reason: Function Increases readability that makes code more organized and easier to understand.
2.) Function in a program increases code reusability
Answer: True
Reason: Function is useful for code reusability, and can be used as many times as we want.
3.) Functions in programming help in reducing code redundancy
Answer: True
Reason: Redundant code i.e. duplicate code is removed and replaced by functions.
4.) A function can return a value
Answer: True
Reason: Return is a value that a function returns whenever it is called
5.) What do we call the variables found in the definition of the function on which further operations may be performed?
Answer: Parameter(s)
Reason: Parameters are the variable that we passed to the functions to perform operations
Standard Questions
1.) Explain five advantages of using functions
Answer: Advantages of using functions:
1.) Function Increases readability of program, which makes code organized and easy to understand.
2.)Functions reduces length of code duplicate code is removed and replaced by functions.
3.) Functions increases reusability of Code, so means it can be used again, and again once created.
4.) Functions reduces the complexity of a program and gives it a simple structure.
5.) Functions makes it simple for us to adjust functionality, and multiple programmers can work on different functions.
2.) What are function parameters?
Answer: Functions parameters are the variables that is passed to the function when it is called to perform set of tasks. Function Parameters are written inside the pair of parentheses in the function definition, which are separated by commas. The names that is given in the function definition are called parameters whereas the values you supply in the function call are called arguments.
For example: A function which is used to add two integer variables, will be having two integers as parameter.
def Area_of_square(a, b):
sum = a + b
Print(sum)
Here, a, b are the function parameters for the function Area of sqaure.
3.) Explain how functions help in reducing redundancy of code.
Answer: A function is a way of reusing a block of code so that it can be used more than once. Functions give the programmer the ability to reduce code redundancy and eliminate hours of wasted work, that means duplicate code can be removed and make the program look more precise and easier to read. Each function allows the code to be centralized, where one change to the function will affect everything that depends on it. Functions can be used to divide and conquer tasks that would seem otherwise impossible to accomplish.
4.) Can functions return a value? Support your answer with proper explanation.
Answer: A function may or may not return a value when called. The return statement returns values from the function. The function performs calculations and display result. We need to send value from the function to its calling function. This is done using return statement.
The return statement does the following:
- returns the control to the calling function.
- return value or None
5.) Name three scenarios where using functions will help you reduce redundancy of code
Answer: Scenarios where using functions will help you reduce redundancy of code:
1.) While creating program to find area of circle we can create a function where we pass radius as parameter and we can calculate area of circle n number of times whenever it is being called, which reduces the code and won’t be any duplicate code.
2.) Program to print even numbers between two intervals, for that we can create a function where we pass two numbers and then it gives us all the even numbers between two intervals, it will help us reduce redundancy of code.
3.) We can sort an array and create a function which returns us the sorted array and thus it helps us reduce redundancy of code and there will be no duplicate and we can sort an array n number of times we just have to call the function.
6.) Explain parameterization of functions. Where can and why do you think parameterization is useful?
Answer: Parameterization is a way of passing data to the function so that the function body use that data and complete the task. Parameters are just simple variables that are defined in function and exist only with the function body. We use parameterization to perform any arithmetic operations, we need to pass variables of any data type as parameters inside the function when it’s been called or invoke.
For Ex – To create a program to find area of circle we need create a function and then pass the radius as a parameter, then the value is going to be used in the function definition part and return the result.
def Areaofcicle(radius):
result = 3.14 * radius * radius
return result
radius = 15
print(“Area of circle is”, Areaofcicle(radius))
7.) Explain what the output of below program will be
def test(x, y):
if x > y:
return y
def test1(x, y, z):
return test(x, test(y, z))
print(test1(2, 7, -1))
Answer: The answer is -1.
Explanation of the given output:
1.) First the test1 function is called where value of x,y,z is 2 ,7 , -1 respectively.
2.) Inside the test1 function test function gets called in return statement where x and y are passed as parameters with value x= 7,y = 1.
3.) Then inside test functions it checks if x > y , if false it returns –1.
4.) And then again inside test1 function test function gets called this time with x and y as parameters but x = 2 and y = -1
5.) And finally, inside test function it checks if x > y if false then then it returns y = -1.
8.) Find an error in the below program:
def sum(numbers):
total = 0
for x in number:
total += x
return total
print (sum (( 1 , 2 , 3, 4 , 5 ) ))
Answer: name number is not defined in the function.
Parameter passed in line 1 is numbers whereas in line 3 when iterating
its written number, which will give error as number is not defined in the
function.
Higher Order Thinking Skills (HOTS)
1.) Write a function to print even numbers from 1 to 50
Answer:
def Even_numbers(l, h):
for i in range(l, h+1):
if I % 2 == 0:
print(i)
L = 1
H = 50
Even_numbers(L, H)
2.) Write a function to do sort the elements [34,12,89,56,98,101] from biggest to smallest.
Answer:
def sort(nums):
n = len(nums)
for i in range(n-1,0,-1):
for j in range(i):
if nums[j]<nums[j+1]:
temp = nums[j]
nums[j] = nums[j+1]
nums[j+1] = temp
alist = [54,26,93,17,77,31,44,55,20]
sort(alist)
print(alist)
3.) Write a function to find the highest number in the array of [1, 87, 09, 18, 11]
Answer:
def maximum(arr):
max = arr[0]
n = len(arr)
for i in range(1, n):
if arr[i] > max:
max = arr[i]
return max
arr = [1, 87, 9, 18, 11]
Largest = maximum(arr)
print (“Largest number in the array is:”, Largest)
4.) Write a function to find the modulus of 3 and 6.
Answer: Function to find modulus of 3 and 6
def mod(num1, num2):
result = num1 % num2
return result
num1 = 3
num2 = 6
res = mod(num1, num2)
print(res)
Applied Project
1.) Create a block code which takes radius and height of a cylinder as input to a function and computes its volume. The volume computed should be returned by the function.
Answer:
2.) Create a block code which computes product of (sum of two random numbers) with the (difference of two random numbers) using functions.