Programming and Computational Thinking
Programming and Computational Thinking
Or
Differentiate between actual
parameter(s) and a formal parameter(s) with a
for each. Suitable example
ICBSE Sample Paper 2020-21]
Ans. Actual Parameter is a
the value from parameter, which is used in
a
function call statement to send
calling function to the called
Formal Parameter is a function. It is also known as Argument.
to
parameter,
receive the value from actual
which is used in
function header of the called function
a
In the above code, actual parameters are 6, 16 and 26; and formal parameters are x, y
and z.
return a +b+C
add (5, 7)
221. What are different types of arguments/parameters that a function can have?
Ans. A function carn have following types of arguments/parameters:
() Positional (regular) arguments. These are the passed argument values in the actual
function call.
arguments, which are copied to formal arguments by their position in the
That is, 1st argument value is given to the 1t parameter, 2ndargumene's value is givent
the 2nd parameter and so on, e.g., in the following code, argument 5 will be given to the
add (5, 7)
return a +b+c
add(5, 7)
add (b 5, a =
7)
76 MOVE FAST WITH COMPUTER SCIENCE (Python) XIl
222. What is the utility of: () default arguments, (ii) keyword arguments?
[Textbook Q, Chapter 3 (Type Al
Ans. (1) The default parameters are parameters with a default value set to them. TL.
default value is automatically considered as the passed value WHEN no value is provid
ed
for that parameter in the function call statement.
Thus default arguments are useful when we want to skip an argument in a functin
call statement and use the default value for it instead.
(17) The keyword arguments give complete control and flexibility over the values sents.
ent as
arguments for the corresponding parameters.
Irrespective of the placement and order of arguments, keyword arguments are corecty
matched.
223. Differentiate between fruitful functions and non-fruitfiul functions.
ITextbook Q, Chapter 3 (Type AJ
Ans. The functions that return a value ie., non-void functions, are also known as
fruitful functions.
The functions that do not return a value, i.e., void functions, are also known as
non-fruitful functions.
224. Can a function return multiple values ? How? ITextbook Q, Chapter 3 (Type A)]
Ans. Yes, a Python function can return more than one value.
To return multiple values from a function, the return statement should have a comma
separated multiple values, eg., following return statement is returning three values:
225. What do you understand by local and global scope of variables ? How can you access a
globa
variable inside the function, if function has a variable with same name. ICBSE SP 19-201
Ans. A global variable is a variable that is accessible globally. A local variable is the one
that is only accessible to the current scope, such as
temporary variables used in a single
function definition.
variable declared outside of all the functions or in
A
global scope is known as global
variable. A global variable can be accessed inside or outside of the function whereas a
local variable can be used only inside of the function. If a
function has a local variable
name as a global variable, then in that function scope, the local variable will hide the
global variable with the same name. We can access a global variable having the same
name as a local variable
by declaring its name with keyword global, e.g., as global A
226. What is the difference between a local variable and a global variable ? Also, give a able
Python code to illustrate both.
ITextbook Q, Chapter 3 (Type
Ans. The differences between a local variable and
global variable are as given belo
Local Variable
Global Variable
1. It is a variable which is declared within a
|It is a variable which is e all
function or within a block declared out
the functions
2. It is accessible only within a function/block in It
which it is declared | is accessible throughout the progra
UNIT I: PROGRAMMING AND COMPUTATIONAL THINKING
77
For example, in the
following code, xCubed
local variables.
x, are global variables and n and Cn are
def cube(n):
cn =n * n*n
return cn
X =10
xCubed cube(x)
print(x, "cubed is", xCubed)
227. Explain the use of global key word used in a function with the help of a suitable example.
ICBSE Sample Paper 2020-21]
Ans. Use of global key word. In Python, global keyword allows the programmer to
use a global variable in the local context. A variable declared inside a function is by default
local and a variable declared outside the function is
global by default. The keyword global
is written inside the function to use its
global value. Outside the function, global keyword
has no effect.
Example
C 10 #global variable
def add ():
global c # now on c will refer to global variable
C =C+2 # global value of c is incremented by 2
print("Inside add():", c)
add()
C 15
print("In main: ", c)
Output
Inside add( ) : 12
In main: 15
Ans.
(ii) None 36
(a) () 48
None 36
229. Consider below given function headers. ldentify which of these will cause error and why
def func(a = 1, b):
()
(i) def func(a = 1, b, c= 2):
Ans. Function headers (), (i) and (iv) will cause error because non-default arguments
cannot follow default arguments.
Only function header (iii) will not cause any error.
230. What are the errors in following codes ? Correct the code and predict output :
Ans.
(a) Wrong indentation for retun statement
Semicolons should be avoided (3 statements have it)
Also, the value returned from the function is not received.
(May or may not be considered an error.)
Corrected code will be:
total =
def sum(arg1, arg2):
total arg1 +arg2
print("Total: ", total)
return total
sum(10, 20) #still the return value not received in a variable
print("Total: ", total)
Output
Total 30
Total: 0
(Note. 2nd line of output because the return value is not received in a variable.)
UNIT I: PROGRAMMING AND COMPUTATIONAL THINKING 79
(b) Range( ) is not a valid function; it should be rangel)
RETURN is not
valid statement. It should be return.
a
Function calls use parenthesis, i.e., O (not square brackets[ ]) to pass vaues
Thus Function calls for
Tot() would be as
Tot(3) and Tot(6)
return y
4
5 def calcSquare(x):
6 a power(x, 2)
7 return a
8
9 n 5
10 result= calcSquare(n)
11 print(result)
Ans.
10->561>2>3>67-> 10 > 11
15 9 ->
ade fragnents