Python Functions: book back answers
24 solved questions from the end of this chapter, Class 12 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Python FunctionsChoose the best answer: (1 Mark)
A named blocks of code that are designed to do one specific job is called as
- (a) Loop
- (b) Branching
- (c) Function
- (d) Block
Try it, then show the answerAnswer
(c) Function
Functions are named blocks of code designed to do a specific job.
From the book: “Functions are named blocks of code that are designed to do specific job.”
A Function which calls itself is called as
- (a) Built-in
- (b) Recursion
- (c) Lambda
- (d) return
Try it, then show the answerAnswer
(b) Recursion
A function that calls itself is known as recursion.
From the book: “When a function calls itself is known as recursion.”
Which function is called anonymous un-named function
- (a) Lambda
- (b) Recursion
- (c) Function
- (d) define
Try it, then show the answerAnswer
(a) Lambda
Anonymous functions are also called lambda functions.
From the book: “Hence, anonymous functions are also called as lambda functions.”
Which of the following keyword is used to begin the function block?
- (a) define
- (b) for
- (c) finally
- (d) def
Try it, then show the answerAnswer
(d) def
A function block begins with the keyword def.
From the book: “Function blocks begin with the keyword “def””
Which of the following keyword is used to exit a function block?
- (a) define
- (b) return
- (c) finally
- (d) def
Try it, then show the answerAnswer
(b) return
The return statement causes a function to exit and returns a value to its caller.
From the book: “The return statement causes your function to exit and returns a value to its caller.”
While defining a function which of the following symbol is used.
- (a) ; (semicolon)
- (b) . (dot)
- (c) : (colon)
- (d) $ (dollar)
Try it, then show the answerAnswer
(c) : (colon)
The function code block comes after a colon.
From the book: “The code block always comes after a colon (:) and is indented.”
In which arguments the correct positional order is passed to a function?
- (a) Required
- (b) Keyword
- (c) Default
- (d) Variable-length
Try it, then show the answerAnswer
(a) Required
Required arguments are passed in the correct positional order.
From the book: ““Required Arguments” are the arguments passed to a function in correct positional order.”
Read the following statement and choose the correct statement(s). (I) In Python, you don’t have to mention the specific data types while defining function. (II) Python keywords can be used as function name.
- (a) I is correct and II is wrong
- (b) Both are correct
- (c) I is wrong and II is correct
- (d) Both are wrong
Try it, then show the answerAnswer
(a) I is correct and II is wrong
The chapter's function syntax does not require declaring parameter data types. It explicitly says Python keywords should not be used as function names.
From the book: “Python keywords should not be used as function name.”
Which of the following keyword is used to define the function testpython(): ?
- (a) define
- (b) pass
- (c) def
- (d) while
Try it, then show the answerAnswer
(c) def
Function blocks begin with the keyword def followed by the function name.
From the book: “Function blocks begin with the keyword “def” followed by function name and parenthesis ().”
Part - II Answer the following questions: (2 Marks)
What is function?
Try it, then show the answerAnswer
A function is a group of related statements that performs a specific task.
Functions are named code blocks dedicated to a particular job.
From the book: “Functions are nothing but a group of related statements that perform a specific task.”
Write the different types of function.
Try it, then show the answerAnswer
User-defined functions, built-in functions, lambda functions and recursion functions.
The chapter divides Python functions into these four types.
From the book: “Types of Functions are User defined, Built-in, lambda and recursion.”
What are the main advantages of function?
Try it, then show the answerAnswer
Functions avoid repetition and enable code reuse. They provide better modularity for an application.
Functions make programs easier to reuse and divide into manageable parts.
From the book: “It avoids repetition and makes high degree of code reusing.”
What is meant by scope of variable? Mention its types.
Try it, then show the answerAnswer
Scope of a variable is the part of a program where it is accessible. Its types are local scope and global scope.
The chapter identifies local and global as the two types of scope.
From the book: “Scope of variable refers to the part of the program, where it is accessible”
Define global scope.
Try it, then show the answerAnswer
A variable with global scope can be used anywhere in the program and can be created outside the scope of any function.
A variable defined outside a function has global scope by default.
From the book: “A variable, with global scope can be used anywhere in the program.”
What is base condition in recursive function
Try it, then show the answerAnswer
The condition applied in a recursive function is called the base condition. It must be present to prevent infinite execution.
When the base condition is met, the program produces meaningful output and exits.
From the book: “The condition that is applied in any recursive function is known as base condition.”
Part - III Answer the following questions: (3 Marks)
Write the rules of local variable.
Try it, then show the answerAnswer
A local variable can be accessed only within the function where it is created. It exists only while that function is executing. Formal parameters are also local to the function.
These are the rules listed for local scope in the chapter.
From the book: “A variable declared inside the function's body is known as local variable.”
Write the basic rules for global keyword in python.
Try it, then show the answerAnswer
A variable defined outside a function is global by default, so the global keyword is not needed to define it. Use global to modify a global variable inside a function. Using global outside a function has no effect.
These are the three basic rules given for the global keyword.
From the book: “We use global keyword to modify the value of the global variable inside a function.”
What happens when we modify global variable inside the function?
Try it, then show the answerAnswer
Without using the global keyword, modifying a global variable inside a function causes an UnboundLocalError. With the global keyword, the function can modify the global variable, and the change is visible outside the function.
The chapter demonstrates both the error without global and a successful modification with global.
From the book: “Without using the global keyword we cannot modify the global variable inside the function”
Differentiate ceil() and floor() function?
Try it, then show the answerAnswer
ceil(x) returns the smallest integer greater than or equal to x. floor(x) returns the largest integer less than or equal to x.
Both are mathematical functions used through the math module.
From the book: “Returns the largest integer less than or equal to x”
What is composition in functions?
Try it, then show the answerAnswer
Composition is using the value returned by one function as an argument for another function in a nested manner.
For example, the chapter describes using eval() on input obtained from input().
From the book: “The value returned by a function may be used as an argument for another function in a nested manner.”
How recursive function works?
Try it, then show the answerAnswer
An external part of the program calls the recursive function. If its base condition is met, it gives meaningful output and exits; otherwise, it processes the task and calls itself again.
The base condition stops the recursive calls; without it, execution can continue like an infinite loop.
From the book: “Otherwise, function does some required processing and then calls itself to continue recursion.”
What are the points to be noted while defining a function?
Try it, then show the answerAnswer
A function block starts with def, followed by the function name and parentheses. Put any parameters inside the parentheses, follow the header with a colon, and indent the code block. Do not use Python keywords as function names.
These are the function-definition requirements stated in the chapter.
From the book: “The code block always comes after a colon (:) and is indented.”
Part - IV Answer the following questions: (5 Marks)
Explain the following built-in functions. (a) id() (b) chr() (c) round() (d) type() (e) pow()
Try it, then show the answerAnswer
Built-in functions are functions inbuilt within Python. The following functions perform specific operations on the values supplied to them.
- id(): Returns the identity of an object, that is, its address in memory.
- chr(): Returns the Unicode character corresponding to the given ASCII value. It is the inverse of ord().
- round(): Returns the nearest integer to the given number. Its optional second argument, ndigits, specifies the number of decimal digits after rounding.
- type(): Returns the type of the given single object.
- pow(): Returns the result of raising the first argument to the power of the second argument, that is, a raised to the power of b.
Example: For a = 5 and b = 2, pow(a,b) returns 25.
These built-in functions return an object’s identity, a character, a rounded value, an object’s type, or a power calculation.
From the book: “Returns the Unicode character for the given ASCII value.”
Explain recursive function with an example.
Try it, then show the answerAnswer
Recursion is a method in which a function calls itself. A recursive function needs a base condition to stop it from continuing indefinitely.
- Meaning: When a function calls itself, it is known as recursion.
- Base condition: The condition applied in a recursive function is called the base condition. It is essential to prevent infinite execution.
- How recursion works: An external part of the program calls the recursive function. If the base condition is met, it produces a meaningful output and exits.
- Continued recursion: If the base condition is not met, the function performs the required processing and calls itself again.
- Use: Recursion works like a loop, and any loop can be converted to recursion.
- Factorial example: The chapter uses a recursive function to calculate factorial: it returns 1 when n is 0; otherwise, it returns n multiplied by the factorial of n−1.
Example: def fact(n): if n == 0: return 1 else: return n * fact(n-1) print(fact(0)) # 1 print(fact(5)) # 120
Thus, recursion solves a task by repeatedly calling the same function until its base condition is met.
From the book: “A recursive function calls itself.”