XII Computer Science EM Five Mark Question and Answer
XII Computer Science EM Five Mark Question and Answer
School, Theni * They do not modify the arguments which are passed
to them.
XII – Computer Science (5M Questions)
let square x:=
Lesson 1 - Function
return: x * x
1) What are called Parameters and write a note on
* The above function square is a pure function
(i) Parameter without Type (ii) Parameter with Type because it will not give different results for same
Parameters are the variables in a function definition input.
and arguments are the values which are passed to a Impure functions
function definition.
* The return value of the impure functions does not
Parameter without Type solely depend on its arguments passed.
Let us see an example of a function definition:
* They may modify the arguments which are passed to
let bigger a b := them.
let randomnumber :=
if a > b then return a
a := random()
else return b
return a
* Variables a and b are parameters. * The above function is an impure function because
* We have not mentioned any data types. the randon()will give different outputs for the same
* Some language compilers solve this type inference function call.
algorithmically. 4. Explain with an example interface and
Parameter with Type implementation.
Now let us write the same function definition with Interface
types. * Interface just defines what an object can do, but
let bigger (a:int) (b:int) :int := won’t actually do it.
if a > b then return a * In object oriented programs classes are the interface
and how the object is processed and executed is the
else return b implementation.
* Here we have mentioned the data types. * For example when you press a light switch, the light
* Parentheses are mandatory to mention the data goes on, you may not have cared how it splashed
type for a and b. the light.
* This is useful on times when you get a type error * A light, should have function definitions like
from the compiler. turn_on () and a turn_off ().
2. Identify in the following program Implementation
let rec gcd a b := * Implementation carries out the instructions defined
in the interface.
if b <> 0 then gcd b (a mod b) else return a
* For example, the person who drives the car doesn't
i) Name of the function – gcd
care about the internal working.
ii) Identify the statement which tells it is a recursive
Internally, the engine of the car is doing all the things.
function – rec keyword
* It's where fuel, air, pressure, and electricity come
iii) Name of the argument variable – a and b
together to create the power to move the vehicle.
iv) Statement which invoke the function recursively –
* Thus we separate interface from implementation.
gcd b (a mod b)
v)Statement which terminates the recursion – return a
3. Explain with example Pure and impure functions.
Pure functions
* Pure functions are functions which will give exact
result when the same arguments are passed.
* The return value of the pure functions solely
depends on its arguments passed.
3. Explain the following built-in functions. (a) id() (b) print("\n GCD = ",gcd)
chr() (c) round() (d) type() (e) pow() print("\n LCM = ",lcm)
(a) id( ) 5. Explain recursive function with an example.
Returns the “identity” or the address of the object in When a function calls itself is known as recursion.
memory. A base condition is must in every recursive function
Ex: x=15 otherwise it will continue to execute like an infinite
print (“address of x is :” , id (x) ) loop.
Output: address of x is : 1357486752 Ex:
(b) chr( ) def fact(n) :
Returns the Unicode character for the given ASCII if n == 1:
value. return 1
else:
Ex: c = 65
return n * fact (n-1)
print (chr (c) )
Output: A print(fact(6))
(c) round( ) The recursion ends when the number reduces to 1.
round() function that rounds off a number to the given This is called the base condition.
number of digits.
Ex: average=76.32
Print(round(average,1))
Output: 76.3