Tamilnadu State Board New Syllabus Samacheer Kalvi 12th Computer Science Guide Pdf Chapter 1 Function Text Book Back Questions and Answers, Notes.

Tamilnadu Samacheer Kalvi 12th Computer Science Solutions Chapter 1 Function

12th Computer Science Guide Function Text Book Questions and Answers

I. Choose the best answer (I Marks)

Question 1.
The small sections of code that are used to perform a particular task is called
a) Subroutines
b) Files
c) Pseudo code
d) Modules
Answer:
a) Subroutines

Question 2.
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
Answer:
b) Function

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 3.
Which of the following is a distinct syntactic block?
a) Subroutines
b) Function
c) Definition
d) Modules
Answer:
c) Definition

Question 4.
The variables in a function definition are called as
a) Subroutines
b) Function
c) Definition
d) Parameters
Answer:
d) Parameters

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 5.
The values which are passed to a function definition are called
a) Arguments
b) Subroutines
c) Function
d) Definition
Answer:
a) Arguments

Question 6.
Which of the following are mandatory to write the type annotations in the function definition
a) Curly braces
b) Parentheses
c) Square brackets
d) indentations
Answer:
b) Parentheses

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 7.
Which of the following defines what an object can do?
a) Operating System
b) Compiler
c) Interface
d) Interpreter
Answer:
c) Interface

Question 8.
Which of the following carries out the instructions defined in the interface?
a) Operating System
b) Compiler
c) Implementation
d) Interpreter
Answer:
c) Implementation

Question 9.
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
Answer:
d) Pure functions

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 10.
The functions which cause side effects to the arguments passed are called
a) Impure functions
b) Partial Functions
c) Dynamic Functions
d) Pure functions
Answer:
a) Impure functions

II. Answer the following questions (2 Marks)

Question 1.
What is a subroutine?
Answer:
Subroutines are the basic building blocks of computer programs. Subroutines are small sections of code that are used to perform a particular task that can be used repeatedly. In Programming languages, these subroutines are called Functions.

Question 2.
Define Function with respect to the Programming language.
Answer:

  • A function is a unit of code that is often defined within a greater code structure.
  • A function works on many kinds of inputs like variants, expressions and produces a concrete output.

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 3.
Write the inference you get from X:=(78).
Answer:
Value 78 is bound to the name X.

Question 4.
Differentiate Interface and Implementation
Answer:

Interface Implementation
Interface defines what an object can do, but won’t actually do it Implementation carries out the instructions defined in the interface

Question 5.
Which of the following is a normal function definition and which is a recursive function definition?
Answer:
(I) Let Recursive sum x y:
return x + y

(II) let disp:
print ‘welcome’

(III) let Recursive sum num:
if (num! = 0) then return num + sum (num – 1) else
return num

  1. Recursive function
  2. Normal function
  3. Recursive function

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

III. Answer the following questions (3 Marks)

Question 1.
Mention the characteristics of Interface.
Answer:

  • The class template specifies the interfaces to enable an object to be created and operated properly.
  • An object’s attributes and behaviour is controlled by sending functions to the object.

Question 2.
Why strlen() is called pure function?
Answer:
strlen (s) is called each time and strlen needs to iterate over the whole of ‘s’. If the compiler is smart enough to work out that strlen is a pure function and that ‘s’ is not updated in the lbop, then it can remove the redundant extra calls to strlen and make the loop to execute only one time. This function reads external memory but does not change it, and the value returned derives from the external memory accessed.

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 3.
What is the side effect of impure function? Give example.
Answer:

  • Impure function has side effects when it has observable interaction with the outside world.
  • The variables used inside the function may cause side effects through the functions which are not passed with any arguments. In such cases the function is called impure function.
  • When a function depends on variables or functions outside of its definition block, you can never be sure that the function will behave the same every time it’s called.
  • For example, the mathematical function random () will give different outputs for the same function call
    let random number
    let a:= random()
    if a> 10 then
    return: a
    else
    return 10
  • Here the function random is impure as it not sure what the will be the result when we call
    the function

Question 4.
Differentiate pure and impure functions.
Answer:

Pure Function

Impure Function

1 The return value of the pure functions solely depends on its arguments passed. The return value of the impure functions does not solely depend on its arguments passed..
2 Pure functions with the same set of arguments always return same values. Impure functions with the same set of arguments may return different values.
3 They do not have any side effects. They have side effects.
4 They do not modify the arguments which are passed to them They may modify the arguments which are passed to them
5 Example: strlen(),sqrt() Example: random(),date()

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 5.
What happens if you modify a variable outside the function? Give an example
Answer:
When a function depends on variables or functions outside of its definition block, you can never be sure that the function will behave the same every time it’s called.
For example let y: = 0
(int) inc (int) x
y: = y + x;
return (y)
In the above example the value of y get changed inside the function defintion due to which the result will change each time. The side effect of the inc ( ) function is it is changing the data ‘ of the external visible variable ‘y’.

IV. Answer the following questions (5 Marks)

Question 1.
What are called Parameters and Write a note on
1. Parameter Without Type
2. Parameter With Type
Answer:
Parameter:
Parameter is the variables in a function definition
Arguments:
Arguments are the values which are passed to a function definition
Parameters passing are of two types namely

  1. Parameter without Type
  2. Parameter with Type

1. Parameter without Type:
Let us see an example of a function definition.
(requires: b > =0 )
(returns: a to the power of b)
let rec pow a b:=
if b=0 then 1
else a pow a*(b-l)

  • In the above function definition variable ‘b’ is the parameter and the value which is passed to the variable ‘b’ is the argument.
  • The precondition (requires) and postcondition (returns) of the function is given.
  • Note we have not mentioned any types (data types).
  • Some language computer solves this type (data type) inference problem algorithmically, but some require the type to be mentioned.

2. Parameter with Type:
Now let us write the same function definition with types for some reason:
(requires: b > 0 )
(returns: a to the power of b )
let rec pow (a: int) (b: int): int: =
if b=0 then 1 else a * pow b (a-1)

  • When we write the type annotations for ‘a’ and ‘b’ the parentheses are mandatory.
  • There are times we may want to explicitly write down types.
  • This useful on times when you get a type error from the compiler that doesn’t make sense.
  • Explicitly annotating the types can help with debugging such an error message.

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 2.
Identify in the following program
Answer:
let rec gcd a b : =
if b < > 0 then gcd b (a mod b) else return a
(I) Name of the function
gcd

(II) Identify the statement which tells it is a recursive function
let rec

(III) Name of the argument variable
a, b

(IV) Statement which invokes the function recursively
gcd b(a mod b) [when b < > 0]

(V) Statement which terminates the recursion
return a (when b becomes 0).

Question 3.
Explain with example Pure and impure functions.
Answer:
Pure functions:

  • Pure functions are functions which will give exact result when the same arguments are passed.
  • For example, the mathematical function sin (0) always results 0.
    Let us see an example.
    let square x
    return: x * x
  • The above function square is a pure function because it will not give different results for the same input.

Impure functions:

  • The variables used inside the function may cause side effects through the functions which are not passed with any arguments. In such cases, the function is called the impure function.
  • When a function depends on variables or functions outside of its definition block, we can never be sure that the function will behave the same every time it’s called.
  • For example, the mathematical functions random () will give different outputs for the same function call.
    let Random number
    let a := random() if a > 10 then
    return: a else
    return: 10
  • Here the function Random is impure as it is not sure what will be the result when we call the function.

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 4.
Explain with an example interface and implementation
Answer:
Interface Vs Implementation:
An interface is a set of actions that an object can do. For example, when you press a light switch, the light goes on, you may not have cared how it splashed the light. In an Object-Oriented Programming language, an Interface is a description of all functions that a class must have in order to be a new interface.

In our example, anything that “ACTS LIKE” a light, should have to function definitions like turn on ( ) and a turn off ( ). The purpose of interfaces is to allow the computer to enforce the properties of the class of TYPE T (whatever the interface is) must have functions called X, Y, Z, etc.

A class declaration combines the external interface (its local state) with an implementation of that interface (the code that carries out the behaviour). An object is an instance created from the class. The interface defines an object’s visibility to the outside world.

Characteristics of interface

  • The class template specifies the interfaces to enable an object to be created and operated properly.
  • An object’s attributes and behavior is controlled by sending functions to the object.
Interface Implementation
Interface defines what an object can do, but won’t actually do it Implementation carries out the instructions defined in the interface

Example:
Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function 1

    • The person who drives the car doesn’t care about the internal working.
  • To increase the speed of the car he just presses the accelerator to get the desired behaviour.
  • Here the accelerator is the interface between the driver (the calling / invoking object) and the engine (the called object).
  • In this case, the function call would be Speed (70): This is the interface.
  • Internally, the engine of the car is doing all the things.
  • It’s where fuel, air, pressure, and electricity come together to create the power to move the vehicle.
    All of these actions are separated from the driver, who just wants to go faster. Thus we separate interface from implementation.

12th Computer Science Guide Function Additional Questions and Answers

I. Choose the best answer (1 Mark)

Question 1.
……………………… are expressed using statements of a programming language.
(a) Algorithm
(b) Procedure
(c) Specification
(d) Abstraction
Answer:
(a) Algorithm

Question 2.
The recursive function is defined using the keyword
a) let
b) requires
c) name
d) let rec
Answer:
d) let rec

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 3.
A function definition which calls itself is called
a) user-defined function
b) recursive function
c) built-in function
d) derived function
Answer:
b) recursive function

Question 4.
Find the correct statement from the following.
(a) a : = (24) has an expression
(b) (24) is an expression
Answer:
(a) a : = (24) has an expression

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 5.
strlen() is an example of ………………. function.
a) pure
b) impure
c) user-defined
d) recursive
Answer:
a) pure

Question 6.
Evaluation of ………………. functions does not cause any side effects to its output?
a) Impure
b) built-in
c) Recursive
d) pure
Answer:
d) pure

Question 7.
The name of the function in let rec pow ab : = is …………………………
(a) Let
(b) Rec
(c) Pow
(d) a b
Answer:
(c) Pow

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 8.
An …………….. is an instance created from the class.
a) Interface
b) object
c) member
d) function
Answer:
b) object

Question 9.
In object-oriented programs …………… are the interface.
a) classes
b) object
c) function
d) implementation
Answer:
a) classes

Question 10.
In b = 0, = is ……………………….. operator
(a) Assignment
(b) Equality
(c) Logical
(d) Not equal
Answer:
(b) Equality

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

II. Answer the following questions (2 and 3 Marks)

Question 1.
What are the two types of parameter passing?
Answer:

  1. Parameter without type
  2. Parameter with type

Question 2.
What is meant by Definition?
Answer:
Definitions are distinct syntactic blocks.

Question 3.
Write the syntax for the function definitions?
Answer:
let rec fn a1 a2 … an : = k
fn : Function name
a1 … an – variable
rec: recursion

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 4.
Define Argument.
Answer:
Arguments are the values which are passed to a function definition through the function definition

Question 5.
Write notes on Interface.
Answer:

  • An interface is a set of actions that an object can do.
  • Interface just defines what an object can do, but won’t actually do it

Question 6.
Define Implementation.
Answer:
Implementation carries out the instructions defined in the interface

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 7.
Write notes on Pure functions.
Answer:

  • Pure functions are functions which will give exact result when the same arguments are passed.
  • Example: strlen(),sqrt()

Question 8.
Write notes on the Impure function.
Answer:

  • The functions which cause side effects to the arguments passed are called
    Impure function.
  • Example: random(), date()

Question 9.
What is a Recursive function?
Answer:
A function definition which calls itself is called a Recursive function.

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 10.
Differentiate parameters and arguments.
Answer:

Parameters

Arguments

Parameters are the variables in a function definition Arguments are the values which are passed to a function definition.

Question 11.
Give function definition for the Chameleons of Chromeland problem?
Answer:
let rec monochromatize abc : =
if a > 0 then
a, b, c : = a – 1, b – 1, c + 2
else
a: = 0, b: = 0, c: = a + b + c
return c

Question 12.
Define Object:
Answer:
An object is an instance created from the class.

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

III. Answer the following questions (5 Marks)

Question 1.
Explain the syntax of function definitions
Answer:

  • The syntax to define functions is close to the mathematical usage.
  • The definition is introduced by the keyword let, followed by the name of the function and its arguments; then the formula that computes the image of the argument is written after an = sign.
  • If you want to define a recursive function: use “let rec” instead of “let”.

The syntax for function definitions:

  • let rec fn al a2 … an := k
  • Here the ‘fn’ is a variable indicating an identifier being used as a
    function name.
  • The names ‘al’ to ‘an’ are variables indicating the identifiers used as parameters.
  • The keyword ‘rec’ is required if ‘fn’ is to be a recursive function; otherwise, it may be omitted.

Question 2.
Write a short note and syntax for function types?
Answer:

  • The syntax for function types
    x→→y
    x1 →→ x2→→y
    x1 →→….. →→xn→→ y
  • The ‘x’ and ‘y’ are variables indicating types
  • The type x →→ ‘y’ is the type of a function that gets an input of type ‘x’ and returns an output of type ‘y’ whereas xl→→ x2 →→y is a type of a function that takes two inputs, the first input is of type and the second input of type ‘xl’, and returns an output of type <y’.
  • Likewise x1→→……. →→ >xn→→y has type ‘x’ as the input of n arguments and ‘y’ type as output

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 3.
On the island, there are different types of chameleons. Whenever two different color chameleons meet they both change their colors to the third color. Suppose two types of chameleons are equal in number.
Construct an algorithm that arranges meetings between these two types so that they change their color to the third type. In the end, all should display the same color.
Answer:
let ree monochromatic a b c:=
if a > 0 then
a, b, c:= a -1, b -1, c + 2
else
a:= 0 b:= 0 c:= a + b + c
return c

HANDS-ON PRACTICE

Question 1.
Write the algorithmic function definition to find the minimum among 3 numbers.
Answer:
let min 3abc:=
if a < b then
if a < c then a else c
else
if b < c then b else c

Samacheer Kalvi 12th Computer Science Guide Chapter 1 Function

Question 2.
Write the algorithmic recursive function definition to find the sum of ‘n ‘ natural numbers. Answer:
let rec sum num:
lf(num!=0)
then return num+sum(num-l)
else return num

Leave a Reply