9789389088137 Flipbook PDF


93 downloads 124 Views 2MB Size

Recommend Stories


Porque. PDF Created with deskpdf PDF Writer - Trial ::
Porque tu hogar empieza desde adentro. www.avilainteriores.com PDF Created with deskPDF PDF Writer - Trial :: http://www.docudesk.com Avila Interi

EMPRESAS HEADHUNTERS CHILE PDF
Get Instant Access to eBook Empresas Headhunters Chile PDF at Our Huge Library EMPRESAS HEADHUNTERS CHILE PDF ==> Download: EMPRESAS HEADHUNTERS CHIL

Story Transcript

12 6

Tamil Nadu CBSE

COMPUTER SCIENCE (AS PER THE LATEST SYLLABUS)

Sh eel a N irmal k umar

B. E., B. Ed. (Computer Science)

FULL MARKS PVT LTD (Progressive Educational Publishers)

CHENNAI - 600 017

Published by:

Full Marks Pvt Ltd # 10/34, Mahalakshmi Street, T. Nagar, Chennai - 600 017 Phone: (044) 24333508; +91 9962012902 E-mail: [email protected] Website: www.fullmarks.org

© Author Al l r i ght s r e s e r ve .d No pa r t o f t hi s ublp i c a t i on m a y eb r e pr duco e d or t r a ns m i t t e ,d i n a yn f or m ro by a ny m e a ns , w i t hout ep r m i s s i on. Any pe r s no w ho oed s a yn una tu hor i s e d a c t i n r e l a t i on t o t hi s publ i c a t i onm a bey l i a bl e t oc r i m i na l pr so e c tu i ona dcn i vi l c l a i m s f or ad m a ge s .

Branches: • Ahmedabad • Bengaluru • Chennai • Dehradun • Guwahati • Hyderabad • Jaipur • Kochi • Kolkata • Lucknow • Mumbai • Ranchi

R E V I SE D E D I T I O N

“ Thi s bok i s m e a nt a l l r e a s nao bl e c a r e ot he r i nt e l l e c t ua l pr ha s / ha ve b e e n una pl e a s e not i f yt he publ

f or

e duc

t o e ns ope r t y bl e t o i s he

a t i ona l a nd ur e t ha t t he c r i gh t s of a ny t r a c k a ny s our r i nw r i t i ngf

l e a r ni ng nto e nt s pe r s on c e a nd ro c or r e

pur

pos

e s . The a ut hor ( s ) of t he bo ok ha of t he okb do not vi lo a t e a ny e xi s t i ng i n a ny m a ne r w ah t s oe ev r . In t he e ve nt i f a yn c opy r i ght ah s be e n i na dve r t e nt c t i ev a c t i on.”

s / ha ev t a ek n c opyr i ght ro t he a ut orh ( s ) l y i nf r i gen d,

Note from the Publishers D ear members of t h e l ear n in g f ra t ern it y , Wa r e pl s ki t he

r m gr e e t i ngs f r om t he Pu bl i s he r s fo F ul l M ra k s. The Comp ut er Sc ien c e X I I gui ed i s e t e w i t h s uc c e s s s t r a t e gi e s a dn t i ps f or de ev l opm e nt of s c hol a r l y a t t i t ude a nd a pl i c a t i on l l s f or s t ude nt s . It ah s eb e n w r i t t e n a c c or di gn t o t he l a t e s t s yl l a usb a nd t e xt okb i s s eu d yb Ta m i l N a duS t a t e B ao r d.

Thi s s upor t bok pr ovi de s c om pl e t e t ut or i a l s upor t t o s t ude nt s t o c ove r t he c our s e c ont e nt s easily, develop confidence and answer the various types of questions generally asked by the B oa r d. Sal ien t F eat ures of t h e book : • The T op ic s Cove redha

ve be

e nm

e nt i one

da

t t he be

gi ni

ngof

e ve r yc

• Complete summary of the chapter is provided in the I mp ort an • All the T ext ual Q uest ion s ha ve be e n s ol ve d a nd have been provided wherever required. • Wherever required, D iagr

am s ha ve be

e ni

nc l ude

• The in-text H na ds on Prac t ic e ha s be e n i nc l ude c on c e pt ua l unde r s t a ndi ng.

df

a l a r ge

num

ha pt e r .

t Poin t s t o R emember. be r of

or a c l e a r unde d t o he l p t he

A ddit ion al r s t a ndi s t ude

Q uest ion s

ng. nt s e nha

nc e t he i r

• The book is up-to-date, dependable and learner-friendly resource.

We r e gi s t e r uro s i nc e r e rg a t i t ude t o t he a ut hor a nd t he t he boki rn e c or td i m e w i t hr e m a r ka ble quality.

s c hol

a r l y e di t or s f or

he l pi ng

us

br i ng

Constructive suggestions for improvement of the quality of this book are most welcome.

( iii)

out

T h e c h ap

t er at

a gl

Topi c s C ove

14

a n c e.. r ed

o tin

Impor t ant

Po

P oi nt s t o R emembe r

7

s in Python

Python Functions

TOPICS COVERED

TOPICS COVERED

1. I ntroduction

1.

I ntroduction

2.

Scripting Language

3.

A pplications of Scripting Languages

4.

F eatures of Python ov er C+ +

5.

I mporting C+ + F iles in Python

6.

Python Program to import C+ +

7 .

Python program E x ecuting C+ + Program using control statement

8 .

How Python is handling the errors in C+ +

9 .

Python program E x ecuting C+ + Program Containing A rrays

10 . Python program E x ecuting C+ + Containing functions

2. Definin

3.

allin a un tion

4.

assin

5.

un tion

6.

non

7 . 9 .

u ents

e etu n tate ent un tions usin li a ies

Program

o e o Va ia les t on e u si e un tions

I mp ort a n t Poin t s t o R emember

11. Python program to I llustrate the inheritance of a Class

v v v

I mp ort a n t Poin t s t o R emember

v

v

C + + i s a c o mpi ler b as ed lan g u ag e w h i le P y t h o n i s an i n t er pr et er b as ed lan g u ag e.

v

v

C + + i s c o mpi led s t at i c ally w h er eas P y t h o n i s i n t er pr et ed d y n ami c ally

v

v

A s t at i c t y ped lan g u ag e li k e C + + r eq u i r es t h e pr o g r ammer t o expli c i t ly t ell t h e c o mpu t er w h at “ d at a t y pe” eac h d at a v alu e i s g o i n g t o u s e.

v

v

A d y n ami c t y ped lan g u ag e li k e P y t h o n , d o es n ’ t r eq u i r e t h e d at a t y pe t o b e g i v en expli c i t ly f o r t h e d at a. P y t h o n man i pu lat e t h e v ar i ab le b as ed o n t h e t y pe o f v alu e.

v

A s c r i pt i n g lan g u ag e i s a pr o g r ammi n g lan g u ag e d es i g n ed f o r i n t eg r at i n g an d c o mmu n i c at i n g w i t h o t h er pr o g r ammi n g lan g u ag es

v

v

Min refers to a set of runtime header les, used in compiling and lin ing the code of C, C + + an d F ORTRAN t o b e r u n o n Wi n d o w s Oper at i n g S y s t em

v

v

Th e d o t ( . ) o per at o r i s u s ed t o ac c es s t h e f u n c t i o n s o f a i mpo r t ed mo d u le

v

s y s mo d u le pr o v i d es ac c es s t o s o me v ar i ab les u s ed b y t h e i n t er pr et er an d t o f u n c t i o n s t h at i n t er ac t w i t h t h e i n t er pr et er

v

OS mo d u le i n P y t h o n pr o v i d es a w ay o f u s i n g o per at i n g s y s t em d epen d en t f u n c t i o n ali t y

v

Th e g et o pt mo d u le o f P y t h o n h elps y o u t o pr o v e ( s pli t ) c o mman d - li n e o pt i o n s an d ar g u men t s .

v v

v v v v

unctions are named bloc s of code that are designed to do one speci c ob. Types of unctions are ser de ned, uilt in, lambda and recursion. unction bloc s begin with the eyword def followed by function name and parenthesis . A return with no arguments is the same as return one. eturn statement is optional in python. n Python, statements in a bloc should begin with indentation. A bloc within a bloc is called nested bloc . Ar g u men t s ar e u s ed t o c all a f u n c t i o n an d t h er e ar e pr i mar i ly 4 t y pes o f f u n c t i o n s t h at o n e can use e uired arguments, eyword arguments, efault arguments and ariable length arguments. e uired arguments are the arguments passed to a function in correct positional order. eyword arguments will invo e the function after the parameters are recognized by their parameter names. A Python function allows us to give the default values for parameters in the function de nition. e call it as efault argument. ariable Length arguments are not speci ed in the function s de nition and an asteris is used to de ne such arguments. Anonymous unction is a function that is de ned without a name. cope of variable refers to the part of the program, where it is accessible, i.e., area where you can refer use it. The value returned by a function may be used as an argument for another function in a nested manner. This is called composition. A function which calls itself is nown as recursion. ecursion wor s li e a loop but sometimes it ma es more sense to use recursion than loop.

Text ua l Exer c i s es T ex t ua l E v a l ua t ion

Sol v ed

PART – I

I . Ch oose t h e best a n sw er: 1. A named bloc s of code that are designed to do one speci c ob is called as ................. . ( a) Loop ( b) ranching ( c) F u n c t i o n ( d) loc . A unction which calls itself is called as ................... . ( a uilt in b ecursion c Lambda d) r et u r n . hich function is called anonymous un named function ( a Lambda b ecursion c) F u n c t i o n ( d de ne . hich of the following eyword is used to begin the function bloc ( a de ne b) f o r ( c nally d) d ef . hich of the following eyword is used to e it a function bloc ( a de ne b) r et u r n ( c nally d) d ef . hile de ning a function which of the following symbol is used. ( a) ; ( s emi c o lo n ) ( b . dot c) : ( c o lo n ) ( d) $ ( d o llar ) . n which arguments the correct positional order is passed to a function ( a e uired b eyword c efault d ariable length . ead the following statement and choose the correct statement s . n Python, you don t have to mention the speci c data types while de ning function. Python eywords can be used as function name. ( a) ( I) i s c o r r ec t an d ( II) i s w r o n g ( b oth are correct ( c) ( I) i s w r o n g an d ( II) i s c o r r ec t ( d oth are wrong . Pic the correct one to e ecute the given statement successfully. if print , is a leap year (a b c d 1 . hich of the following eyword is used to de ne the function testpython ( a de ne b) pas s ( c) d ef ( d) w h i le Answers

1. c) F u n c t i o n

. b

ecursion

. b return

. c

colon

.a

. a) ( I) i s c o r r ec t an d ( II) i s w r o n g

9 .b

. a Lambda

. d) d ef

e uired 1 . c) d ef

PART – II

I I . A n sw er t h e fol l ow in g q uest ion s: 1 .

8 . 10 .

un tions a a ete s in un tions ous un tions

W h a t is fun c t ion ? unctions are named bloc s of code that are designed to do specific ob. f you need to perform that tas multiple times throughout your program, you ust call the function dedicated to t on un tions

(iv )

101

t i ona l Q ue s t i ons

D i agr ams &

E x a mp l e

O ut p ut

print test print oesn t print Python print Python , n , Lang.. print Python , t , Lang..

test oesn t Python P y t h o n Lan g . . P y t h o n Lan g . .

Gr aphs

7. Read t h e c o d e: ( a) i mpo r t mat plo t li b . py plo t as plt ( b) plt . plo t ( 3, 2) ( c) plt . s h o w ( ) Id en t i f y t h e o u t pu t f o r t h e ab o v e c o d i n g .

( a)

( b)

Epic Info

16

ADDITIONAL QUESTIONS

1

16 14

14

info

12

Hands

y axis

Y axis

12

I. C h o o se t h e b est a nswer 1. P y t h o n lan g u ag e w as r eleas ed i n t h e y ear . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ( a) 19 9 1 ( b) 19 9 3 ( c) 19 9 5 ( d) 19 9 7 2. C WI mean s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3. P y t h o n g o t i t s n ame f r o m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4. F i n d t h e w r o n g s t at emen t f r o m t h e f o llo w i n g . ( a) P y t h o n s u ppo r t s pr o c ed u r al appr o ac h es ( b) P y t h o n s u ppo r t s o b j ec t o r i en t ed appr o ac h es ( c) P y t h o n i s D BMS t o o l 5. ID LE mean s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6. Ho w man y mo d es o f pr o g r ammi n g ar e t h er e i n py t h o n ? ( a) 2 ( b) 3 ( c) 4 ( d) 5 . The e tension for python le is ........................................... . ( a) . py t ( b) . pt ( c) . py ( d) . py t h mode is used to create and edit python source le 8. ( a) li n e ( b) s c r i pt ( c) In t er ac t i v e ( d) In t er f ac e 9 . Wh i c h mo d e c an b e u s ed as a s i mple c alc u lat o r ? ( a) li n e ( b) s c r i pt ( c) In t er ac t i v e ( d) In t er f ac e 10. Wh at d o es pr o mpt ( > > > ) i n d i c at o r ? ( a) c o mpi ler i s r ead y t o d eb u g ( b) r es u lt s ar e r ead y ( c) w ai t i n g f o r t h e In pu t d at a ( d) In t er pr et er i s r ead y t o ac c ept In s t r u c t i o n s 11. Wh i c h c o mman d i s u s ed t o g et o u t pu t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ( a) c o u t ( b) pr i n t ( c) pr i n t f ( d) w r i t e 12. F i n d t h e c o r r ec t o n e f r o m t h e f o llo w i n g . ( a) s c r i pt s ar e r eu s ab le an d n o t ed i t ab le ( b) s c r i pt s ar e n o t r eu s ab le an d t h ey ar e ed i t ab le ( c) s c r i pt s ar e n o t r eu s u ab le, n o t ed i t ab le ( d) s c r i pt s ar e b o t h r eu s ab le an d ed i t ab le 1 . hich command is selected from le menu create new script te t editor ( a) New ( b ew le c) New ed i t o r ( d ew cript le 1 . hat is the default name for blan script te t editor ( a) u n t i t led ( b) u n t i t led 1 ( c) d o c u men t 1 ( d) ed i t o r 1 1 . hat is the eyboard shortcut to un ( a) F 5 ( b) Alt + F 5 ( c) S h i f t + F 5 ( d) C t r l + F 5

10

10

4

8 6

2 5

7

6

8

X axis

3

3

4

5

7

6

x axis

( d)

2.100

4.0 3.5

2.025 2.000 1.975 1.950

3.0 2.5 2.0 1.5

1.925

1.0

1.900 2.85

2.90

2.95

3.00

3.05

3.10

8. Wh i c h k ey i s u s ed t o r u n t h e mo d u le? ( a) F 6 ( b) F 4 tify t 1: t 2: Li n

2

11

2.050

0.0

3.15

( c) F 3

0.0

0.5

1.5

2.0

2.5

3.0

( d) F 5

t h e r i g h t t y pe o f c h ar t u s i n g t h e f o llo w i n g h i n t s . Th i s c h ar t i s o f t en u s ed t o v i s u ali z e a t r en d i n d at a o v er i n t er v als o f t i me. Th e li n e i n t h i s t y pe o f c h ar t i s o f t en d r aw n c h r o n o lo g i c ally . e c h ar t ( b) Bar c h ar t ( c) P i e c h ar t ( d) S c at t er plo t

10. Read t h e s t at emen t s g i v en b elo w . Id en t i f y t h e r i g h t o pt i o n f r o m t h e f o llo w i n g f o r pi e c h ar t . S t at emen t A: To mak e a pi e c h ar t w i t h Mat plo t li b , w e c an u s e t h e plt . pi e( ) f u n c t i o n . S t at emen t B: Th e au t o pc t par amet er allo w s u s t o d i s play t h e per c en t ag e v alu e u s i n g t h e P y t h o n s t r i n g f o r mat t i n g . ( a) S t at emen t A i s c o r r ec t ( b) S t at emen t B i s c o r r ec t ( c) Bo t h t h e s t at emen t s ar e c o r r ec t ( d) Bo t h t h e s t at emen t s ar e w r o n g

on P

r ac t i c e

te any fi e enefits n s n od la o a n . he ene ts o s n od la o a n n l de • Less code to be written. • A single procedure can be developed for reuse, eliminating the need to retype the code many times. • Programs can be designed more easily because a small team deals with only a small part of the entire code. • Modular programming allows many programmers to collaborate on the same application. • The code is stored across multiple files. • Code is short, simple and easy to understand. • Errors can easily be identified, as they are localized to a subroutine or function. • The same code can be used in many applications. • The scoping of variables can easily be controlled. ands on P a t e

1.

10

2.075

( v ) Mo d u le s eg men t s c an b e u s ed b y o t h er mo d u les . 3.

9

( c)

9 . Id en Hi n Hi n ( a)

8

2 6

some numbers

Addi

E sc a p e seq uen c e D esc rip t ion c h a ra c t er ac slash ingle uote ouble uote n New li n e t Tab

se e the ollo n d a a and

te the se do ode o the ollo n .

s u m( ) : n u m1: = 20 s u m1( ) n u m1: = n u m1 + 10 s u m2( ) n u m1: = n u m1 + 10 s u m2( ) s u m1( ) n u m1: = 10 s u m( ) Print num1

(v )

Contents UNIT- I – Problem Solving Techniques 1.

F un c t ion . 2.

D at a A bst rac t ion . 3.

Sc op in g . 4.

A l gor it h mic St rat egi es .

7 . 18 27 . 39

UNIT- II – Core Python 5.

Pyt h on - V ar iab l es an 6.

Con t rol St ruc t ures . 7.

Pyt h on fun c t ion s . 8.

St rin gs an

d O p erat ors .

54 80 9

.

d St rin g m an

ip ul at ion s .

UNIT-III – Modularity and OOPS

1 1 6

9. 10.

L ist s, T up l es, S et s an Pyt h on Cl as ses an

d D ic t ion ar y .

d obj ec t s .

15

13

UNIT-IV – Database concepts and MySql 1 1 . D at ab as e Con c ep t s . 12.

St ruc t ured Q uery L

an

167 gu ag

e ( SQ L ) .

13. Python and CSV files .

203

184

UNIT-V – Integrating Python with MySql and C++ 14.

I mp ort in g C + + p rogr am s in Pyt h on .

27

15.

D at a m an

251

16.

D at a vi c h ar t an

ip ul at ion t h rough

sual iz at ion usin g p d bar c h ar t .

SQ L . y p l ot : l in e c h ar t , p ie 27 .

( vi)

1

Function

TOPICS COVERED 1. I ntroduction

2. F unction with respect to Programming language

3. I nterface Vs I mplementation

4. Pure functions

5

I mpure functions

Important Points to Remember v v v v v v v v v v v v

Algorithms are expressed using statements of a programming language. Subroutines are small sections of code that are used to perform a particular task that can be used repeatedly. A function is a unit of code that is often defined within a greater code structure. A function contains a set of code that works on many kinds of inputs and produces a concrete output. Definitions are distinct syntactic blocks. Parameters are the variables in a function definition and arguments are the values which are passed to a function definition through the function definition. hen you write the type annotations the parentheses are mandatory in the function definition An interface is a set of action that an object can do. Interface ust defines what an ob ect can do, but won t actually do it. Implementation carries out the instructions defined in the interface. Pure functions are functions which will give exact result when the same arguments are passed. 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. Textual Evaluation Solved PART – I

I. Choose the best answer: 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 . hich 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 7

. . 5. 6. 7. . .

1 .

hich of the following is a distinct syntactic block (a) Subroutines (b) Function (c) Definition (d) Modules The variables in a function definition are called as ......................... . (a) Subroutines (b) Function (c) Definition (d) Parameters The values which are passed to a function definition are called .......................... . (a) Arguments (b) Subroutines (c) Function (d) Definition hich of the following are mandatory to write the type annotations in the function definition (a) Curly braces (b) Parentheses (c) Square brackets (d) indentations hich of the following defines what an ob ect can do (a) Operating System (b) Compiler (c) Interface (d) Interpreter hich of the following carries out the instructions defined in the interface (a) Operating System (b) Compiler (c) Implementation (d) Interpreter The functions which will give exact result when same arguments are passed are called ............. . (a) Impure functions (b) Partial Functions (c) Dynamic Functions (d) Pure functions The functions which cause side effects to the arguments passed are called ..................... . (a) Impure functions (b) Partial Functions (c) Dynamic Functions (d) Pure functions Answers

1. (a) Subroutines 5. (a) Arguments 9. (d) Pure functions

2. (b) Function 6. (b) Parentheses 10. (a) impure function

3. (c) Definition 7. (c) Interface

4. (d) Parameters 8. (c) Implementation

PART – II

II. Answer the following questions: 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. Define Function with respect to Programming language. A function is a unit of code that is often defined within a 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 : Value 78 being bound to the name X .

.

4. Differentiate interface and implementation. Interface Interface ust defines what an ob ect can do, but won t actually do it. 8

Computer Science -12

Implementation Implementation carries out the instructions defined in the interface.



. Which of the following is a normal function definition and which is recursive function definition. i let ecursive sum x y: return x y ii let disp : print welcome iii let ecursive sum num: if num then return num sum num 1 else return num (i) Recursive function (ii) Normal function (iii) Recursive function PART – III

III. Answer the following questions: 1. Mention the characteristics of Interface. Characteristics of interface • The class template specifies the interfaces to enable an ob ect to be created and operated properly. • An ob ect s attributes and behaviour is controlled by sending functions to the ob ect. 2. Why strlen is called pure function? 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. 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. ive example. Impure Function ✦ The return value of the impure functions does not solely depend on its arguments passed. Hence, if you call the impure functions with the same set of arguments, you might get the different return values. or example, random(), Date(). ✦ They may modify the arguments which are passed to them. 4. Differentiate pure and impure function. S. N o. Pure Function 1. The return value of the pure functions solely depends on its arguments passed. 2. If you call the pure functions with the same set of arguments, you will always get the same return values. 3. They do not have any side effects. 4. They do not modify the arguments which are passed to them.

Impure Function The return value of the impure functions does not solely depend on its arguments passed. If you call the impure functions with the same set of arguments, you might get the different return values. or example, random(), Date(). They have side effects. They may modify the arguments which are passed to them. F unction

9

. What happens if you modify a variable outside the function? ive an example. hen 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 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 defintion 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 . PART – IV

IV . Answer the following questions: 1. What are called Parameters and write a note on i Parameter without ype ii Parameter with ype Parameters and arguments Parameters are the variables in a function definition and arguments are the values which are passed to a function definition. i Parameter without ype Let us see an example of a function definition: (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. ote 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, by the typing rule the entire if expression has type int. Since the if expression has type ‘ int’ , the function s return type also be int . ‘ b’ is compared to 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. ii Parameter with ype ow let us write the same function definition with types for some reason requires: b returns: a to the power of b let rec pow a: int b: int : int : if b then 1 else a pow b a 1 10

Computer Science -12

2.











hen 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. xplicitly 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” . Syntax: The syntax for function definitions: let rec fna1 a2 ... an : k Here the ‘ f n’ is a variable indicating an identifier being used as a function name. The names ‘ a1’ to ‘ an’ are variables indicating the identifiers used as parameters. The keyword ‘ rec’ is required if ‘ f n’ is to be a recursive function otherwise it may be omitted. or example let us see an example to check whether the entered number is even or odd. requires: x let rec even x : x odd x 1 return even requires: x let odd x : x even x 1 return odd The syntax for function types: x → y x1 → x2 → y x1 → ... → xn → y The x and y are variables indicating types. The type x → y is the type of a function that gets an input of type x and returns an output of type y . hereas x1 → x → y is a type of a function that takes two inputs, the first input is of type x1 and the second input of type x , and returns an output of type y . Likewise x1 → → xn → y has type x as input of n arguments and y type as output. Identify in the following program let rec gcd a b : if b then gcd b a mod b else return a i ame of the function gcd ii Identify the statement which tells it is a recursive function let rec iii ame of the argument variable a, b iv Statement which invoke the function recursively gcd b(a mod b) when b F unction

11

v Statement which terminates the recursion return a (when b becomes ) 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. or example the mathematical function sin ( ) always results . 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 squar e 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. Let 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. rom 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 through the functions which are not passed with any arguments. In such cases the function is called impure function. hen 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. or 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. 4. Explain with an example interface and implementation. Interface Vs Implementation An interface is a set of action that an ob ect can do. or example when you press a light

12



Computer Science -12

switch, the light goes on, you may not have cared how it splashed the light. In Ob ect Oriented Programming language, an Interface is a description of all functions that a class must have in order to be a new interface. In our example, anything that " A CT S LI K E " a light, should have function defintions like turn on () and a turn off (). The purpose of interfaces is to allow the computer to enforce the properties of the class of T Y P E T (whatever the interface is) must have functions called X , Y , Z , etc. A class declaration combines the external interface (its local state) with an implementation of that interface (the code that carries out the behaviour). An object is an instance created from the class. The interface defines an ob ect s visibility to the outside world. The difference between interface and implementation is Interface Implementation Interface ust defines what an ob ect can do, Implementation carries out the instructions but won t actually do it defined in the interface In ob ect oriented programs classes are the interface and how the ob ect is processed and executed is the implementation. Characteristics of interface The class template specifies the interfaces to enable an object to be created and operated properly. An ob ect s attributes and behaviour is controlled by sending functions to the ob ect. or example, let s take the example of increasing a car s speed. ENGINE

getSpeed

required speed

No

Pull Fuel

Yes

Return Return

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 between the driver (the calling invoking ob ect) and the engine (the called ob ect). In this case, the function call would be Speed (7 ) This 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 ust wants to go faster. Let us see a simple example, consider the following implementation of a function that finds the minimum of its three arguments: F unction

13

let min 3 x y z := if x y then if x z then x else z else if y z then y else z Hands on Practice 1. Write algorithmic function definition to find the minimum among 3 numbers. Ans. let min 3 x y z := if x y then if x z then x else z else if y z then y else z 2. Write algorithmic recursive function definition to find the sum of n natural numbers. Ans. let rec sum num: if (num!=0) then return num + sum (num-1) else return num ADDITIONAL QUESTIONS PART – I

I. C h o o se t h e b est a nswer 1. ......................... are expressed using statements of a programming language. (a) Algorithm (b) procedure (c) Specification (d) Abstraction 2. .................... are the basic building blocks of a computer programs. (a) code (b) subroutines (c) modules (d) variables . In programming languages, subroutines are called as ................................ . (a) Functions (b) Task (c) Modules (d) Code . ind the correct statement from the following. (a) a : = (24) has an expression (b) (24) is an expression 5. ......................... binds values to names. (a) Algorithms (b) Variables (c) Interface (d) Definitions 6. Identify the statement which is wrong. (a) Definitions are expressions (b) Definitions are distinct syntactic blocks. (c) Definitions can have expressions, nested inside them. 7. The name of the function in let rec pow ab is .......................... . (a) let (b) rec (c) pow (d) a b . In function definition pre condition is given by ............................. . (a) needed (b) let (c) returns (d) requires . In function definition post condition is given by ............................ . (a) needed (b) let (c) returns (d) requires 14

Computer Science -12

1 . In b , is ............................... operator (a) Assignment (b) equality (c) logical (d) not equal 11. The formula should be written after ............... sign (a) + (b) (c) = (d) + + 1 . To define a recursive function, .................... is used. (a) let (b) let r (c) let rfn (d) let rec 1 . ind which is false. (a) All function definitions are static (b) All function definitions are dynamic 1 . A ........................ combines the external interface with an implementation of that interface. 15. An ..................... is an instance created from the class. (a) object (b) Functions (c) Subroutines (d) definitions 16. ind the statement which is not true. (a) The interface defines an ob ects visibility to the outside world (b) Interface defines what an ob ect can do. (c) In ob ect oriented programs, ob ects are interfaces 17. An ................... attributes and behaviour is controlled by sending functions to the object 1 . The class template specifies the ................ to enable an ob ect to be created and operated properly. 1 . The accelerator is the ............................ between the driver and the engine. (a) interface (b) object (c) instruction (d) code 20. sin(0) = 0 is an example for ............................ function. (a) Impure (b) pure (c) Interface (d) Instruction 1. ind the impure function from the following. (a) sin(0) (b) square x (c) strlen (s) (d) none of these 22. The function random () is an example for ..................... functions. . hy is the function random() is a impure function (a) It gives different outputs for same function call (b) It gives different outputs when is given (c) It will not give different output . hich function definition, doesn t modify the arguments passed to them (a) pure function (b) Impure function (c) object (d) Interface 5. How many parameters are defined in the function let rec gcd a b (a) 0 (b) 1 (c) 2 (d) 3 6. In the function definition, the keyword let is followed by (a) function name (b) arguments (c) parameters (d) implementations 7. ind the correct statement from the following function definitions. let rec p on a b : = (a) data type of the parameters are given (b) data type of the parameters are not mentioned . If a function is not a recursive one, then ............................... is used (a) abc (b) gcd (c) let (d) let rec 29. Find the name of the function. let rec even x : = (a) let (b) rec (c) even (d) x F unction

15

Full Marks Pvt Ltd (Progressive Educational Publishers)

10/34, Mahalakshmi Street, T. Nagar, Chennai-600017 Phone: 044-24333508 E-mail: [email protected] Website: www.fullmarks.org

Get in touch

Social

© Copyright 2013 - 2024 MYDOKUMENT.COM - All rights reserved.