Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

12th computer science

Rating
-
Sold
-
Pages
79
Uploaded on
05-05-2024
Written in
2023/2024

Lecture notes of 79 pages for the course Python at Python (..)

Institution
Python
Course
Python

Content preview

Chapter -1

Part – II

1. What is a subroutine?
 subroutines are 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) has an expression in it but (78) is not itself an expression.
 X:=(78) is a function definition.
 Definitions bind values to names.
 Hence, the value 78 bound to the name “X‟.
4. Differentiate Interface and Implementation?

Interface Implementation
Interface just defines what an Implementation carries out the
object can do,but won‟t actually instructions defined in the
do it interface.
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 behaviour is controlled by sending functions to the object.
2. Why Strlen is called pure function?
 strlen() is a pure function
 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.

, 3. What is the side effect of impure function. Give example.
Impure Function has the following side effects,
▪ Function impure (has side effect) is that it doesn‟t take any arguments and it doesn‟t return
any value.
▪ Function depends on variables or functions outside of its definition block.
▪ It never assure you that the function will behave the same every time it‟s called.
Example:
let y: = 0
(int) inc (int) x
y: = y + x;
return (y)
▪ Here, the result of inc() will change every time if the value of “y” get changed inside the
function definition.
Hence, the side effect of inc () function is changing the data of the external variable “y”.

Differentiate pure and impure function.

Pure Function Impure Function
Pure functions are functions which will Impure functions are functions which will
give exact result when the same not give exact result when the same
arguments are called. Ex. sin(0) arguments are called Ex. date()
The return value depends on its The return value does not depend on its
arguments passed. arguments passed
same return values. different return values
They do not have any side effects. They have side effects.
They do not modify the arguments which They may modify the arguments which
are passed to them are passed to them
1. What happens if you modify a variable outside the function? Give an example.
 Modifying the variable outside of function causes side effect.
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 to changing the data of the external visible
variable „y‟
Part-IV
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.
 Arguments are the values which are passed to a function definition.

Syntax for function definition:
let rec fn a1 a2 ... an := k
fn


Parameter without Type Here , data types are not mentioned with parameter in
function definition.
Ex. let rec pow a b:=
if b=0 then 1

, else a * pow a (b-1)
Some language compiler solves this type (data type) problem algorithmically, but
some require the type to be mentioned.

(ii)Parameter with Type Here , data types are mentioned with parameter in function definition.
Ex . let rec pow (a: int) (b: int) : int :=
if b=0 then 1
else a * pow a(b-1)
Explicitly annotating the types can help with debugging such an error message.
2. Explain with example pure and impure function
Pure Function
 Pure functions are functions which will give exact result when the same
arguments are called. Ex. sin(0)
 The return value depends on its arguments passed.
 same return values.
 They do not have any side effects.
 They do not modify the arguments which are passed to them
Example
 strlen() is a pure function
 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 Function
 Impure functions are functions which will not give exact result when the same
arguments are called Ex. date()
 The return value does not depend on its arguments passed.
 Different return values
 They have side effects.
 They may modify the arguments which are passed to them.
 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.
Example:
let Random number
let a := random()
if a > 10 then
return: a
else
return: 10

3. Identify in the following program
let rec gcd a b :=
if b <> 0 then gcd b (a mod b) else return a
i) Name of the function
ii) Identify the statement which tells it is a recursive function
iii) Name of the argument variable

, iv) Statement which invoke the function recursively
v) Statement which terminates the recursion
Answers
(i) gcd
(ii) let rec gcd a b
(iii) a and b are two arguments passed to the gcd function.
(iv) gcd b (a mod b)
(v) return a.
4. Explain Interface Vs Implementation with example.
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
● The interface defines an object‟s visibility to the outside world.
● In Object Oriented Programming language, an Interface is a description of all functions that a
class must have inorder to be a new interface.
● The purpose of interfaces is to allow the computer to enforce the properties of the class of
TYPE T (whatever the interface is) must have functions called X, Y, Z, etc.
● For example when you press a light switch, the light goes on, you may not have cared how it
splashed the light.
● In our example, anything that "ACTS LIKE" a light, should have function
definitions like turn_on () and a turn_off ().
Characteristics of interface
● The class template specifies the interfaces to enable an object to be created and operated
properly.
● An object's attributes and behaviour is controlled by sending functions to the object.
Implementation
● Implementation carries out the instructions defined in the interface
● In object oriented programs ,how the object is processed and executed is the implementation.
● For example, let's take the example of increasing a car‟s speed.
● The person who drives the car doesn't care about the internal working.
● To increase the speed of the car he just presses the accelerator to get the desired behaviour.
● Here the accelerator is the interface
● Internally, the engine of the car is doing all the things.
● It's where fuel, air, pressure, and electricity come together to create the power to move the
vehicle.
● All of these actions are separated from the driver, who just wants to go faster.
● Thus, we separate interface from implementation.
Implementation of a function that finds the minimum of its three arguments
let min x y z :=
if x < y then
if x<z then x
else
if y<z then y else z
Hands On Practice
1. Write an algorithmic function definition to find the minimum among 3 numbers.
let min x y z :=
if x < y then
if x<z then x
else
if y<z then y else z
2. Write algorithmic recursive function definition to find the sum of n natural numbers.
let rec sum num :=
if (num!=0) then return num+sum(num-1)
else

Document information

Uploaded on
May 5, 2024
Number of pages
79
Written in
2023/2024
Type
Class notes
Professor(s)
Archana
Contains
12
$10.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
khariharankrish

Get to know the seller

Seller avatar
khariharankrish
View profile
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions