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

CS Functions

The document consists of a series of questions and answers related to programming concepts, specifically focusing on functions, parameters, arguments, and object-oriented programming principles. It includes multiple-choice questions, short answer questions, and explanations of terms such as pure and impure functions, interfaces, and implementations. The document serves as an educational resource for students at Don Bosco Matriculation Hr. Sec. School in Chennai.

Uploaded by

25s25h25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CS Functions

The document consists of a series of questions and answers related to programming concepts, specifically focusing on functions, parameters, arguments, and object-oriented programming principles. It includes multiple-choice questions, short answer questions, and explanations of terms such as pure and impure functions, interfaces, and implementations. The document serves as an educational resource for students at Don Bosco Matriculation Hr. Sec. School in Chennai.

Uploaded by

25s25h25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Don Bosco Matriculation Hr. Sec.

School
# 13, Casa Major Road, Egmore, Chennai – 600 008.

+2
Functions
PART - I
I. Choose the best answer : (1 mark)
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
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
3. Which of the following is a distinct syntactic block?
(a) Subroutines (b) Function (c) Definition (d) Modules
4. The variables in a function definition are called as
(a) Subroutines (b) Function (c) Definition (d) Parameters
5. The values which are passed to a function definition are called
(a) Arguments (b) Subroutines (c) Function (d) Definition
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
7. Which of the following defines what an object can do?
(a) Operating System (b) Compiler (c) Interface (d) Interpreter
8. Which of the following carries out the instructions defined in the interface?
(a) Operating System (b) Compiler (c) Implementation (d) Interpreter
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
10. The functions which cause side effects to the arguments passed are called
(a) impure function (b) Partial Functions (c) Dynamic Functions (d) Pure functions
11. Which of the following are the basic building blocks of computer programs ?
(a) Subroutines (b) Procedures (c) Sub function (d) Modules
12. In programming languages, subroutines are called
(a) files (b) modules (c) functions (d) procedures
13. Which of the following contains a set of code that works on many kinds of inputs like variants expressions
and produces a concrete output ?
(a) files (b) modules (c) functions (d) procedures
14. Which bind values to names ?
(a) subroutines (b) function (c) modules (d) definitions
15. In a function definition, which of the following keywords specifies the precondition ?
(a) let (b) requires (c) returns (d) rec
16. In a function definition, which of the following keywords specifies the post condition ?
(a) let (b) requires (c) returns (d) rec
17. The function definition is introduced by the keyword
(a) let (b) requires (c) returns (d) rec
18. If we want to define a recursive function, we have to use the keyword ___________.
(a) let (b) requires (c) let rec (d) requires
19. A function definition which calls by itself is called as
(a) nested function (b) repeating function (c) recursive function (d) self function
20. In Object Oriented Programming language, which is a description of all functions that a class must have in
order to be a new interface ?
(a) interface (b) implementation (c) compiler (d) interpreter
21. Which declaration combines the external interface with an implementation of that interface ?
(a) an array (b) a structure (c) a class (d) a pointer
-2-
22. ⁠ An instance created from the class is called as
(a) variable (b) member (c) tag (d) object
23. ⁠ Which defines an object’s visibility to the outside world ?
(a) interface (b) implementation (c) compiler (d) interpreter
24. ⁠In Object Oriented Programs, which of the following are the interfaces ?
(a) structures (b) arrays (c) classes (d) pointers
25. ⁠ In Object Oriented Programs, classes are the
(a) interface (b) implementation (c) compiler (d) interpreter
26. ⁠ In Object Oriented Programs, how the object is processed and executed is the
(a) interface (b) implementation (c) compiler (d) interpreter
27. ⁠ Which of the following is not a pure function?
(a) sin( ) (b) square( ) (c) strlen( ) (d) random( )
28. ⁠ Which of the following functions do not modify the arguments which are passed to them ?
(a) pure functions (b) impure functions (c) recursive functions (d) none of these
29. ⁠ Which of the following statements are true of false ?
(i) All functions are static definitions (ii) There is no dynamic function definitions
(a) (i)-False (ii)-False (b) (i)-True (ii)-False (c) (i)-False (ii)-True (d) (i)-True (ii)-True
30. Which of the following specifies the interfaces to enable an object to be created and operated properly ?
(a) class template (b) structure template (c) array (d) pointers

PART - II
II. Answer the following questions : (2 marks)
1. What is a subroutine ?
▪ 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 as Functions.
2. Define Function with respect to Programming language.
▪ 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.
3. Write the inference you get from X:=(78).
▪ X:=(78) is a function definition.
▪ Definitions bind values to names
▪ Hence, he value 78 being bound to the name ‘X’.
4. Differentiate interface and implementation.

Interface Implementation
Interface just defines what an Implementation carries out the
object can do, but won’t actually instructions defined in the interface
do it
5. Differentiate between parameters and arguments.
parameters arguments
Parameters are the variables in a Arguments are the values which are
function definition passed to a function definition.
6. Which of the following is a normal function definition and which is recursive function definition.
i) let sum x y:
return x + y Normal Function

ii) let disp :


print ‘welcome’ Normal Function
-3-
iii) let rec sum num:
if (num!=0) then return num + sum (num-1)
else
Recursive Function
return num

PART - III
III. Answer the following questions : (3 marks)
1. Mention the characteristics of Interface.
▪ 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.
▪ let's take the example of increasing a car’s speed. Here the accelerator is the interface between the
driver (the calling / invoking object) and the engine (the called object).
2. Why strlen is called pure function?
▪ strlen is a pure function because the function takes one variable as a parameter, and accesses it to find
its length.
▪ This function reads external memory but does not change it, and the value returned derives from the
external memory accessed.
▪ Evaluation of pure functions does not cause any side effects to its output.
3. What is the side effect of impure function. Give example.
Impure function has the following side effects :
▪ 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 function random() will give different outputs for the same function call.
4. Differentiate pure and impure function.

5. What happens if you modify a variable outside the function? Give an example.
One of the most powerful groups of side effects is modifying the variable outside the function.
▪ the value of y get changed inside the function definition 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’.
▪ As you can see some side effects are quite easy to spot and some
of them may tricky.
▪ A good sign that our function impure (has side effect) is that it
doesn’t take any arguments and it doesn’t return any value.
-4-
PART - IV
IV. Answer the following questions : (5 marks)
1. What are called Parameters and write a note on (i) Parameter without Type (ii) Parameter with Type.
Parameters are the variables in a function definition. Two types of parameter passing are (i) Parameter
without Type (ii) Parameter with Type.
(i) Parameter without Type
Let us see an example of a function definition:
• 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 post condition (returns)
of the function is given.
• we have not mentioned any types: (data types). This is
called Parameter without Type.
• In this function definition, the expression has type ‘int’, so
the function’s return type will also be ‘int’ by implicit.

(ii) Parameter with Type


Now let us write the same function definition with types for some reason:
• In this example, we have explicitly annotated the type of
argument and return type as ‘int’.
• Here, when we write the type of annotations for ‘a’ and
‘b’ the parenthesis is mandatory.
• Generally we can leave out these annotations, because
it's simpler to let the compiler infer them.

▪ There are times we may want to explicitly write down types.


▪ This is 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.
2. Identify in the following program
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 gcd a b :=
‘rec’ keyword tells the compiler – it is a recursive function

(iii) Name of the argument variable


‘a’ and ‘b’
(iv) Statement which invoke the function recursively
gcd b (a mod b)
(v) Statement which terminates the recursion
return a
-5-
3. Explain with example Pure and impure functions.

Pure Functions
▪ Pure functions are functions which will give exact result when the same arguments are passed.
▪ The mathematical function sin (0) always results 0. This means that every time you call the function with
the same arguments, you will always get the same result.
▪ A function can be a pure function provided it should not have any external variable which will alter the
behaviour of that variable.
example
let square x
return: x * x
The above function square is a pure function because it will not give different results for same input.
▪ if a function is pure, then if it is called several times with the same arguments, the compiler only needs
to actually call the function once.
example
▪ strlen (s) is called each time and strlen needs to iterate
over the whole of ‘s’.
▪ strlen is a pure function because the function takes one
variable as a parameter, and accesses it to find its length.
▪ This function reads external memory but does not change
it, and the value returned derives from the external
memory accessed.
Impure Functions

▪ The variables used inside the function may cause side effects though the functions which are not passed
with any arguments. This 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.
▪ Here the function Random is impure as it is not sure what will be the
result when we call the function.

Pure Functions Impure Functions


The return value of the pure functions The return value of the impure functions does
solely depends on its arguments passed. not solely depend on its arguments passed.
They do not modify the arguments which They may modify the arguments which are
are passed to them. passed to them.

4. Explain with an example interface and implementation.


▪ An interface is a set of action that an object can do.
▪ The interface defines an object’s visibility to the outside world.
▪ For example when you press a light switch, the light goes on, you may not have cared how it splashed
the light.
▪ In our example, anything that "ACTS LIKE" a light, should have function definitions like turn_on () and a
turn_off ().
-6-

▪ The difference between interface and implementation is


Interface Implementation
Interface just defines what an object Implementation carries out the
can do, but won’t actually do it instructions defined in the interface
Characteristics of interface
▪ 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.
For example, let's take the example of increasing a car’s speed.
▪ 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.
5. Write algorithmic function definition to find the minimum among 3 numbers.

let min3 x y z :=
if x < y then
if x < z then
return(x)
else
return(z)
else
if y < z then
return(y)
else
return(z)
6. Write algorithmic recursive function definition to find the sum of n natural numbers.

let rec sum n:=


if n <= 1
return(n)
else
return(n) + sum(n-1)

You might also like