0% found this document useful (0 votes)
16 views5 pages

12 CS Unit 1

The document covers fundamental concepts in computer science, focusing on subroutines, functions, parameters, and the distinction between interfaces and implementations. It explains pure and impure functions, providing examples to illustrate their characteristics and behaviors. Additionally, it discusses the importance of type annotations and the role of side effects in function purity.

Uploaded by

bhuv.013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

12 CS Unit 1

The document covers fundamental concepts in computer science, focusing on subroutines, functions, parameters, and the distinction between interfaces and implementations. It explains pure and impure functions, providing examples to illustrate their characteristics and behaviors. Additionally, it discusses the importance of type annotations and the role of side effects in function purity.

Uploaded by

bhuv.013
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ST.

JOHN'S MATRIC HR SEC SCHOOL-TNR


XII COMPUTER SCIENCE
UNIT 1
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

2. Define Function with respect to Programming language.


Answer:-
A function is a unit of code that is often defined within a greater code structure. Specifically, a
function contains a set of code that works on many kinds of inputs, like variants, expressions and
produces a concrete output.

3. Write the inference you get from X:=(78).


Answer:-
Let us consider the example X:= (78). X:= (78) has an expression in it but (78) is not itself an
expression. Rather, it is a function definition. Definitions bind values to names, in this case the
value 78 being bound to the name ‘a’. Definitions are not expressions, at the same time
expressions are also not treated as definitions.
4. What are called Parameters and write a note on
(i) Parameter without Type (ii) Parameter with Type
Answer:-
Parameters (and arguments) Parameters are the variables in a function definition and arguments
are the values which are passed to a function definition.
1. Parameter without Type
(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-1)

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 compiler
solves this type (data type) inference problem algorithmically, but some require the type to be
mentioned.
In the above function definition if expression can return 1 in the then branch, shows that as per
the typing rule the entire if expression has type int. Since the if expression is of type ‘int’, the
function's return type also be ‘int’. ‘b’ is compared to 0 with the equality operator, so ‘b’ is also
a type of ‘int’. Since ‘a’ is multiplied with another expression using the * operator, ‘a’ must be
an int.
(ii) Parameter with Type

(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 a (b-1)

When we write the type annotations for ‘a’ and ‘b’ the parentheses are 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.

5. Difference between Interface and Implementation


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

6. Write 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.

7. Why strlen is called pure function?


Answer:-
let length s:=
i: = 0
if i <strlen (s) then
-- Do something which doesn't affect s
++i

If it is compiled, 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 loop, then it can remove the redundant extra calls to strlen and make the loop to execute
only one time. From these what we can understand, 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.
8. Differentiate pure and impure function.

9. Explain with an example interface and implementation.


Answer:-
Interface - Interface just defines what an object can do, but won’t actually do it
Implementation- Implementation carries out the 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.
Let us see a simple example, consider the following implementation of a function that finds the
minimum of its three arguments:
let min x y z :=
if x < y then
if x < z then x else z
else
if y < z then y else z
10. 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.
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 same
input.There are various theoretical advantages of having pure functions. One advantage is that 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. Let’s see an example
let length s:=
i: = 0
if i <strlen (s) then
-- Do something which doesn't affect s
++i
If it is compiled, 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 loop, then it can remove the redundant extra calls to strlen and make the loop to execute
only one time. From these what we can understand, 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. 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 randomnumber :=
a := random()
if a > 10 then
return: a
else
return: 10
Side-effects (Impure functions)
As you are aware function has side effects when it has observable interaction with the outside
world. There are situations our functions can become impure though our goal is to make our
functions pure. Just to clarify remember that side effect is not a necessary bad thing.Sometimes
they are useful (especially outside functional programming paradigm).
Modify variable outside a function
y: = 0
let inc (x: int): int:=
y: = y + x
return (y)
In the above example 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.

You might also like