0% found this document useful (0 votes)
45 views

Programming and Computational Thinking

A function can return multiple values by returning a tuple (collection of values). Python allows functions to return multiple objects by returning them as a tuple. For example, a function that calculates the area and perimeter of a rectangle could return both values in a single tuple: def calc_area_perimeter(length, width): area = length * width perimeter = 2 * (length + width) return (area, perimeter) This allows the function to return the area and perimeter together rather than having to use multiple return statements or global variables. The calling code would then receive and work with the tuple of returned values. Functions in Python can thus return multiple computed values in a convenient tuple format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Programming and Computational Thinking

A function can return multiple values by returning a tuple (collection of values). Python allows functions to return multiple objects by returning them as a tuple. For example, a function that calculates the area and perimeter of a rectangle could return both values in a single tuple: def calc_area_perimeter(length, width): area = length * width perimeter = 2 * (length + width) return (area, perimeter) This allows the function to return the area and perimeter together rather than having to use multiple return statements or global variables. The calling code would then receive and work with the tuple of returned values. Functions in Python can thus return multiple computed values in a convenient tuple format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

PROGRAMMING AND COMPUTATIONAL THINKING 75

220. What is the


diference between the formal
alternative names? Also, parameters and
give a suitable Python code to actual parameters? What are their
illustrate both.
Or
What is the difference between an
argument and parameter?
a

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

parameter. It is also known as Parameter.


For example,

def addEm(x, y, z):


print (x +y + Z)
addEm (6, 16, 26)

In the above code, actual parameters are 6, 16 and 26; and formal parameters are x, y
and z.

221. What are


different types of arguments/parameters that a
function can have?
Ans. A function have
can
following types of arguments/parameters:
(i) Positional (regular) arguments. These are the passed argument values in the actual
arguments, which are copied to formal arguments by their position in the function call.
That is, 1st argument value is given to the 1st parameter, 2nd argument's value is given to
the 2nd parameter and so on, e,g., in the following code, argument 5 will be given to the
parameter a and argument 7 will be given to the parameter b.

def add (a, b):


return a +b
add(5, 7)
(ii) Default Arguments. These are the default values defined in the function definition
for a parameter. Python uses these detaults if corresponding actual arguments are not
passed in the function call, e.g, in the following code, the third argument is passed and
hence its default value 3 will be taken by the function for the third parameter c

def add(a, b, c = 3):

return a +b+C

add (5, 7)

(iii) Keyword Arguments. Also


called named arguments, the Keyword arguments are

as Opposed to positional arguments in the


passed by it's name instead of their position
function call and the position of arguments is irrelevant when calling a function, e,g,

def add (a, b) :


return a +b
add(b 5 , a = 7)
UNIT I: PROGRAMMING AND COMPUTATIONAL THINKINGG
75
220. What is the diference between the formal and actual parameters?
alternative names? Also, give a suitable parameters
What are their

Python code to illustrate both.


Or
What is the difference between an
argument and a
parameter ?
Or
Differentiate between actual parameter(s) and a formal parameter(s) with a suitable example
for each.
ICBSE Sample Paper 2020-21]
Ans. Actual Parameter is a
parameter, which is used in a function call statement to send
the value from calling function to the called
function.
It is also known as Argument.
Formal Parameter is a parameter, which is used in a
function header of the called function
to receive the value from actual parameter. It is also known as Parameter.
For example,

def addEm(x, y, z):


print(x+y+ z)
addEm(6, 16, 26)
In the above code, actual parameters are 6, 16 and 26; and formal parameters are x,y
and z.

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

parameter a and 7 will be given to the parameter b.


argument
def add(a, b):
return a +b

add (5, 7)

the default values defined in the function definition


(i) Default Arguments. These are if corresponding actual arguments are not
defaults
for a parameter. Python uses these
in the following code, the third argument is passed and
passed in the function call, e.g., the function for the third parameter c
hence its default value 3 will be taken by
def add(a, b, c = 3):

return a +b+c

add(5, 7)

Also called named arguments, the Keyword arguments


are
(ii1) Keyword Arguments. in the
name instead of
their position as Opposed to positional arguments
passed by it's
when calling a functiorn, e.g.,
of arguments is irrelevant
function call and the position
def add(a, b):
return a +b

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:

return 23, (a + b), 3**5

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

228. (a) What will the following codes print?

(i) def addEm(x, y, z):


() def addEm(x, y, z)
print(x+y+ z) K X+y+Z
def prod (x, y, z)
def prod (x, y, z) :

return x *y * z return x*y*z


a = addEm(6, 16, 26)
a =addEm(6, 16, 26)
3, 6)
b prod ( 2, 3, 6)
b=prod ( 2, print(a, b)
print(a, b)

the void and non-void functions. The previous code


stores
(b) In the part (a)'s code, identijy in variables. Why did Python not
both void and non-v014 functions
the return values of
did not return a value ?
an error
when void funcfions
report
Xll
78 MOVE FAST WITH COMPUTER SCIENCE (Python)

Ans.
(ii) None 36
(a) () 48
None 36

(6) Void function addEm()


Non-void function prod()
In Python, void functions do not return a value; rather they report the absence nc
returning value by returning None, which is legal empty value in Python. Thus, variable a
stores None and it is not any error.

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):

(ii) def func(a 1, b =1, c= 2):


(iv) def func(a = 1, b=1, c = 2, d):

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 :

(a) total =0; ) def Tot(Number): #Method to find Total


def sum( arg1, arg2 ): Sum
total = arg1 + arg2; for Cin Range (1, Number +1) :
print("Total: ", total) Sum +C
return total; RETURN Sum
sum( 18, 20 ); print (Tot[3]) #Function Calls
print("Total ", total) print (Tot[6])
ITextbook Q, Chapter 3 (Type B]

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)

Corrected code will be:


def Tot (Number)
Sum
for C in range(1, Number
+1):
Sum+= C
return Sum
print (Tot (3))
print(Tot(6))
Output
6
21
231. Consider the following code and write the flow of execution for this. Line numbers have
ITextbook Q, Chapter 3 (Type
given for your reference.
1 def power(b, p):
y = b ** p

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

You might also like