12th computer science important questions 2022 pdf
12th computer science important questions 2022 pdf |
Chapter 1 FUNCTION
1.What are called Parameters and write a note on
(i) Parameter without Type (ii) Parameter with Type
Parameters
Parameters are the variables in a
function defi nition and arguments are
the values which are passed to a function
defi nition.
Parameter without Type
Let us see an example of a function
defi nition:
(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.
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 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.
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โ.
2.Explain with example Pure and impure functions.
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. 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.
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. Ltโs see an
example
let 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 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.
Leave a Reply