Chapter 3 – Working with Functions
Type A: Short Answer Questions / Conceptual Questions
Question – 1: Program having functions vs Program not having a function? Why?
Answer: A program having multiple functions is considered better designed than a program without any functions, because –
(a) It makes program handling easier.
(b) It avoids ambiguity.
(c) It reduces the program size,
(d) It makes a program more readable and understandable to a programmer.
Question – 2: Information was given by Function Header.
Answer: The first line of the function definition is called Function Header. It begins with the keyword def and ends with a colon (:), specifies the name of the function, parameters and default argument’s value, if any.
Question – 3: Understand the flow of execution
Answer: The Flow of execution refers to the order in which statements are executed during a program run.
Question – 4: Define – Arguments & Parameters. How are Arguments related to Parameters?
Answer: Arguments – In Python, the values are passed to a function through a function call, called Arguments.
Parameters – In Python, the values are received by a function through the function header/definition called Parameters.
Arguments are used in the function call statement while parameters are used in the function header.
Arguments and Parameters are related to each other in the following ways –
- (i) The values of arguments are passed to the Parameters.
- (ii) In some cases, parameters and arguments are used interchangeably.
- (iii) In some cases, the function can change the value of the argument through parameters.
Question – 5: Utility of (a) Default Arguments (b) Keyword Arguments
Answer: (a) Default Arguments – Default arguments are used to assign a default value to the parameters, in case parameters do not receive any value through argument, they can use their default value.
(b) Keyword Arguments – Keyword arguments are the named arguments with assigned values being passed in the function call statement.
Question – 6: Example of default arguments and keyword arguments
Answer: Code of Default arguments and Keyword arguments are
def dSum(a, b = 20): return a + b def kSum(a = 10, b=20): return a+b print(dSum(100)) #using the default value print(kSum(b = 40, a = 30)) #using the keyword argument
Question – 7: Different styles of Functions in Python
Answer: Different Styles of Function in Python are
#Type sof function isn Python #Positional Argument def pSum(a, b): return a+b #Default Argument & Positional Argument def dSum(a, b = 20): return a + b #Default Argument and keyword argument def kSum(a = 10, b=20): return a+b #Variable length argument def vSum(*v): t = 0 for n in v: t += n return t print(pSum(5,10)) #using positional argument print(dSum(100)) #using the default value print(kSum(b = 40, a = 30)) #using the keyword argument print(vSum(5,10,14,2,6)) #using variable length argument