CS Functions
CS Functions
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
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.
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.
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.