Functions and Function Specifications: book back answers
17 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 Functions and Function SpecificationsHands on Practice
Write algorithmic function definition to find the minimum among 3 numbers.
Try it, then show the answerAnswer
let min x y z := if x < y then if x < z then x else z else if y < z then y else z
The function compares the three arguments and returns the smallest one.
From the book: “consider the following implementation of a function that finds the minimum of its three arguments:”
Part - I Choose the best answer (1 Mark)
The small sections of code that are used to perform a particular task is called
- (A) Subroutines
- (B) Files
- (C) Pseudo code
- (D) Modules
Try it, then show the answerAnswer
(A) Subroutines
The chapter calls small, reusable sections of code that perform a particular task subroutines.
From the book: “Subroutines are small sections of code that are used to perform a particular task”
Which of the following is a unit of code that is often defined within a greater code structure?
- (A) Subroutines
- (B) Function
- (C) Files
- (D) Modules
Try it, then show the answerAnswer
(B) Function
The chapter defines a function as a unit of code often defined within a greater code structure.
From the book: “A function is a unit of code that is often defined within a greater code structure.”
Which of the following is a distinct syntactic block?
- (A) Subroutines
- (B) Function
- (C) Definition
- (D) Modules
Try it, then show the answerAnswer
(C) Definition
The chapter explicitly says that definitions are distinct syntactic blocks.
From the book: “Definitions are distinct syntactic blocks.”
The variables in a function definition are called as
- (A) Subroutines
- (B) Function
- (C) Definition
- (D) Parameters
Try it, then show the answerAnswer
(D) Parameters
Variables in a function definition are parameters.
From the book: “Parameters are the variables in a function definition”
The values which are passed to a function definition are called
- (A) Arguments
- (B) Subroutines
- (C) Function
- (D) Definition
Try it, then show the answerAnswer
(A) Arguments
The chapter names values passed to a function definition as arguments.
From the book: “arguments are the values which are passed to a function definition.”
Which of the following are mandatory to write the type annotations in the function definition?
- (A) { }
- (B) ( )
- (C) [ ]
- (D) < >
Try it, then show the answerAnswer
(B) ( )
Parentheses are mandatory when writing type annotations for the parameters.
From the book: “the parentheses are mandatory.”
Which of the following defines what an object can do?
- (A) Operating System
- (B) Compiler
- (C) Interface
- (D) Interpreter
Try it, then show the answerAnswer
(C) Interface
An interface defines what an object can do.
From the book: “Interface just defines what an object can do”
Which of the following carries out the instructions defined in the interface?
- (A) Operating System
- (B) Compiler
- (C) Implementation
- (D) Interpreter
Try it, then show the answerAnswer
(C) Implementation
Implementation carries out the instructions described by the interface.
From the book: “Implementation carries out the instructions defined in the interface”
The functions which will give exact result when same arguments are passed are called
- (A) Impure functions
- (B) Partial Functions
- (C) Dynamic Functions
- (D) Pure functions
Try it, then show the answerAnswer
(D) Pure functions
Pure functions give the same exact result when called with the same arguments.
From the book: “Pure functions are functions which will give exact result when the same arguments are passed.”
Part - II Answer the following questions (2 Marks)
What is a subroutine?
Try it, then show the answerAnswer
A subroutine is a small section of code used to perform a particular task and which can be used repeatedly.
The chapter describes subroutines as reusable code sections for particular tasks.
From the book: “Subroutines are small sections of code that are used to perform a particular task”
Define Function with respect to Programming language.
Try it, then show the answerAnswer
A function is a unit of code, often defined within a greater code structure, that works on inputs and produces a concrete output.
A function processes inputs such as variables or expressions and produces an output.
From the book: “a function contains a set of code that works on many kinds of inputs”
Differentiate interface and implementation.
Try it, then show the answerAnswer
An interface defines what an object can do, but does not carry out the action. An implementation carries out the instructions defined in the interface.
The interface describes available actions, while the implementation performs them.
From the book: “Interface just defines what an object can do, but won’t actually do it”
Which of the following is a normal function definition and which is recursive function definition i) let sum x y: return x + y ii) let disp : print ‘welcome’ iii) let rec sum num: if (num!=0) then return num + sum (num-1) else return num
Try it, then show the answerAnswer
i) Normal function definition. ii) Normal function definition. iii) Recursive function definition.
A definition that calls itself is recursive; the third definition calls sum from within sum. The first two do not call themselves.
From the book: “A function definition which call itself is called recursive function.”
Part - III Answer the following questions (3 Marks)
Why strlen is called pure function?
Try it, then show the answerAnswer
strlen is pure because it reads the string to find its length without changing it, and the returned value derives from that string.
The chapter explains that strlen accesses external memory but does not modify it; its result comes from the accessed value.
From the book: “This function reads external memory but does not change it”
Part - IV Answer the following questions (5Marks)
What are called Parameters and write a note on (i) Parameter without Type (ii) Parameter with Type
Try it, then show the answerAnswer
Parameters are the variables given in a function definition. Arguments are the values passed to those variables.
- Parameter without Type: A function can be defined without specifying the data types of its parameters. In the example, b is a parameter; its data type is inferred from how it is used in the function.
- Example without Type: The function pow is written without type annotations for a and b. It calculates a to the power of b when b is non-negative.
- Type Inference: Some language compilers infer the types algorithmically. In the pow example, the operations show that both a and b must be of type int, and the return type is also int.
- Parameter with Type: Types may be explicitly written for the parameters and the function's return value. Parentheses are mandatory when writing the type annotations for a and b.
- Example with Type: The same pow function is written with a and b as int parameters and int as its return type.
- Why Specify Types: Type annotations can usually be omitted because the compiler can infer them. Explicit annotations can help when debugging a compiler type error that is difficult to understand.
Example: let rec pow (a: int) (b: int) : int := if b=0 then 1 else a * pow a (b-1)
Parameters may be written without types for the compiler to infer, or with explicit type annotations.
From the book: “Parameters are the variables in a function definition”
Explain with an example interface and implementation.
Try it, then show the answerAnswer
An interface describes the actions an object can perform, while an implementation carries out those actions. Separating them lets a user operate an object without knowing its internal working.
- Interface: An interface defines what an object can do, but does not carry out the action itself.
- Implementation: The implementation performs the instructions specified by the interface.
- In object-oriented programs: Classes provide the interface, while the processing and execution of an object form its implementation.
- Interface in the car example: The driver uses the accelerator to request an increase in speed. The accelerator acts as the interface between the driver and the engine.
- Implementation in the car example: The engine performs the internal work needed to move the vehicle, which is hidden from the driver.
Example: To increase a car’s speed, the driver presses the accelerator. The accelerator is the interface, and the engine’s internal work is the implementation. The function call Speed (70) is the interface.
Thus, an interface specifies the action available, while its implementation performs that action.
From the book: “Here the accelerator is the interface between the driver”