Saturday, 5 February 2022

12th Computer Science Chapter 1 Reduced Syllabus Guide - Functions

12th Computer Science Chapter 1 Reduced Guide

12th Computer Guide

 1. FUNCTIONS 

Section – A 

 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) Curly braces 

(B) Parentheses 

(C) Square brackets 

(D) indentations 


Section-B 

Answer the following questions (2 Mark) 

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.

2. Define Function with respect to Programming language. 

  • A function is a unit of code that is often defined within a greater code structure. 
  • A function works on many kinds of inputs and produces a concrete output 

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

  • X:=(78) is a function definition.  
  • Definitions bind values to names.  
  • Hence, the value 78 bound to the name „X‟.
5. Which of the following is a normal function definition and which is recursive function definition?
i) let rec sum x y:
return x + y
Ans: Recursive Function
ii) let disp :
print „welcome‟
Ans: Normal Function
iii) let rec sum num:
if (num!=0) then return num + sum (num-1)
else
return num
Ans: Recursive Function

Section - D

Answer the following questions: (5 Mark)

1. What are called Parameters and write a note on
(i) Parameter without Type (ii) Parameter with Type
Answer:
Parameters are the variables in a function definition. Arguments are the values which are passed to a function definition.

Two types of parameter passing are,
1. Parameter Without Type
2. Parameter With Type

1. Parameter Without Type:

Lets see an example of a function definition of 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 passed to the variable
‘b’ is the argument.
  • The precondition (requires) and postcondition (returns) of the function is given.
  • We have not mentioned any types: (data types). This is called parameter without type.
  • In the above function definition the expression has type ‘int’, so the function's return type also be ‘int’ by implicit.

2. Parameter With Type:

Now let us write the same function definition with types,

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

  • In this example we have explicitly annotating the types of argument and return type as ‘int’.
  • Here, when we write the type annotations for „a‟ and „b‟ the parantheses are mandatory.
  • This is the way passing parameter with type which helps the compiler to easily infer them.

No comments:

Post a Comment

12th Bio Botany Chapter 2 Important Questions And Notes

12th Botany notes  CHAPTER-2 CLASSICAL GENETICS 1. Write the importance of variations (1) Variations make some individuals better fitted in ...