KEMBAR78
12th CompSci Chapter 1 Function | PDF | Parameter (Computer Programming) | Programming Paradigms
0% found this document useful (0 votes)
35 views4 pages

12th CompSci Chapter 1 Function

Uploaded by

chirankamal29
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)
35 views4 pages

12th CompSci Chapter 1 Function

Uploaded by

chirankamal29
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/ 4

UNIT – I CHAPTER -1 FUNCTION

Part -II
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. What is a function with respect to the programming language?
A function is a unit of code that is often defined within the 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).
X:=78 has an expression in it but (24) is not itself and expression. Rather, it
is a function definition.
Definitions bind values to names, in this case the value 24 being bound to
the name ‘a’.
Definitions are not expressions, at the same time expressions are also not
treated as definitions.
Definitions are distinct syntactic blocks. Definitions have expressions nested
inside them, and vice-versa.
4. What do you mean by parameters and arguments?
Parameters are the variables in a function definition and arguments are the
values which are passed to a function definition.

5.Differentiate interface and implementation.

Interface Implementation

Interface just defines what Implementation carries out


an object can do, but won’t the instructions defined in
actually do it. the interface.
6. Which of the following is a normal function definition and which is recursive
function definition?
i) let sum x y:
return x + y
ii) let disp:
print ‘ welcome’
iii) let rec sum num:
if(num!=0) then return num + sum(num-1)
else
return num
Answer:
i) Normal function ii) Normal function iii) Recursive function
Part –III
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 behavior is controlled by sending functions
to the object.
2. Why strlen is called pure function?
Strlen(s) is a pure function because the function takes one variable as a
parameter, and access 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.
So Strlen(s) function will give exact result when the same arguments are
passed.

3. What is the side effect of impure function. Give Example.


The variables used inside the function may cause side effects though the
functions which are not passed with any arguments. In such cases the functions 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.
Example : random() function
Date(), time ()
4. Differentiate pure and impure function.

PURE FUNCTION IMPURE FUNCTION


The return value of the pure The return value of the impure
function solely depends on its functions does not solely depend
arguments passed. Hence, if you on its arguments passed. Hence, if
call the pure functions with the you call the impure functions with
same set of arguments, you will the same set of arguments, you
always get the same return values. might get the different return
values.
They do not have any side effects. They have side effects.
The do not modify the arguments They may modify the arguments
which are passed to them. which are passed to them.
Example. Example:
Strlen(), pow(), random()
sin(), square() date(), time ()

5. What happens if you modify a variable outside the function? Give an


example.
One of the most popular groups of side effects is modifying the variable
outside of function.
For Example.
let y:=0
(int) inc (int) x:
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. A good sign that our function impure ( has side effect)
is that it doesn’s take any arguments and it doesn’t return any value.

Part –III

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 and arguments are the
values which are passed to a function.
i) 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 the post condition (returns) of the function
is given.
We have not mentioned any types :(data types).
Some language compiler soles this type(data type) inference problem
algorithmically, but some requires 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 if of type ‘int’, the function’s return type also be’int’. Since ‘a’ is
multiplied with another expression using the * operator, ‘a’ must be an int.
ii) Parameter with type
Now let me write the same function definition
(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 this annotation, 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.

You might also like