Data Abstraction
Data Abstraction
each time and strlen needs to iterate over as it is not sure what will be the result when
the whole of ‘s’. If the compiler is smart we call the function.
enough to work out that strlen is a pure
1.4.2 Side-effects (Impure functions)
function and that ‘s’ is not updated in the
loop, then it can remove the redundant As you are aware function has side
extra calls to strlen and make the loop to effects when it has observable interaction
execute only one time. From these what we with the outside world. There are situations
can understand, strlen is a pure function our functions can become impure though
because the function takes one variable as a our goal is to make our functions pure.
parameter, and accesses it to find its length. Just to clarify remember that side effect is
This function reads external memory but not a necessary bad thing.Sometimes they
does not change it, and the value returned are useful (especially outside functional
derives from the external memory accessed. programming paradigm).
Modify variable outside a function
Note One of the most popular side effects is
Evaluation of pure modifying the variable outside of function.
functions does not cause any side
For example
effects to its output
y: = 0
1.4.1 Impure functions let inc (x: int): int:=
y: = y + x
The variables used inside the
return (y)
function may cause side effects though the
functions which are not passed with any
In the above example the value of y
arguments. In such cases the function is
get changed inside the function definition
called impure function. When a function
due to which the result will change each
depends on variables or functions outside
time. The side effect of the inc () function is
of its definition block, you can never be
it is changing the data of the external visible
sure that the function will behave the same
variable ‘y’. As you can see some side effects
every time it’s called. For example the
are quite easy to spot and some of them may
mathematical function random() will give
tricky.
different outputs for the same function call.
From all these examples and
let randomnumber :=
definitions what we can understand about
a := random() the main differences between pure and
if a > 10 then impure functions are
return: a
else
return: 10
5 Function
monochromatize (a, b, c)
Now let’s see the example of a pure
function to determine the greatest common
-- inputs : a = A, b = B, c = C, a = b
divisor (gcd) of two positive integer numbers.
-- outputs : a = b = 0, c = A+B+C
let rec gcd a b :=
if b <> 0 then gcd b (a mod b) In each iterative step, two chameleons
else
of the two types (equal in number) meet and
return a
change their colors to the third one. For
output
example, if A, B, C = 4, 4, 6, then the series
gcd 13 27
1
of meeting will result in
gcd 20536 7826
2 iteration a b c
0 4 4 6
In the above example ‘gcd’ is the name
of the function which recursively called till 1 3 3 8
the variable ‘b’ becomes ‘0’. Remember b
and (a mod b) are two arguments passed to 2 2 2 10
‘a’ and ‘b’ of the gcd function. 3 1 1 12
4 0 0 14
Points to remember:
• Algorithms are expressed using statements of a programming language
• Subroutines are small sections of code that are used to perform a particular task that
can be used repeatedly
• A function is a unit of code that is often defined within a greater code structure
• A function contains a set of code that works on many kinds of inputs and produces a
concrete output
• Definitions are distinct syntactic blocks
• Parameters are the variables in a function definition and arguments are the values
which are passed to a function definition through the function definition.
• When you write the type annotations the parentheses are mandatory in the function
definition
• An interface is a set of action that an object can do
• Interface just defines what an object can do, but won’t actually do it
• Implementation carries out the instructions defined in the interface
• Pure functions are functions which will give exact result when the same arguments
are passed
• 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
7 Function
Evaluation
Part - 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) { } (B) ( ) (C) [ ] (D) < >
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
Part - III
9 Function
REFERENCES
13 Data Abstraction