+ All Categories
Home > Documents > Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Date post: 21-Dec-2015
Category:
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
29
Introduction to Introduction to Computing Computing Dr. Nadeem A Khan Dr. Nadeem A Khan
Transcript
Page 1: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Introduction to Introduction to ComputingComputing

Dr. Nadeem A KhanDr. Nadeem A Khan

Page 2: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Lecture 18Lecture 18

Page 3: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Passing Subprograms: Passing Variables Variables

►Example 1: SubroutineExample 1: Subroutine

Sub Triple( num As Single)Sub Triple( num As Single)Rem Triples a numberRem Triples a numberPicture1.Print num;Picture1.Print num;Let num = num*3Let num = num*3Picture1.Print num;Picture1.Print num;End SubEnd Sub

Page 4: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Passing Subprograms: Passing Variables Variables

►Example 1: Program Example 1: Program

Sub Command1_Click( )Sub Command1_Click( )Dim amt As SingleDim amt As SinglePicture1.ClsPicture1.ClsLet amt=2Let amt=2Picture1.Print amt;Picture1.Print amt;Call Triple(amt)Call Triple(amt)Picture1.Print amtPicture1.Print amtEnd SubEnd Sub

Page 5: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Passing Subprograms: Passing Variables Variables

►Example 1: Result Example 1: Result

2 2 6 62 2 6 6

=> Variable was passed by reference in => Variable was passed by reference in Example 1Example 1

Page 6: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Variable Passing by Val!Variable Passing by Val!

Page 7: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Passing Subprograms: Passing Variables Variables

►Example 2: Program Example 2: Program

Sub Command1_Click( )Sub Command1_Click( )Dim amt As SingleDim amt As SinglePicture1.ClsPicture1.ClsLet amt=2Let amt=2Picture1.Print amt;Picture1.Print amt;Call Triple((amt))Call Triple((amt)) ‘extra parentheses‘extra parenthesesPicture1.Print amtPicture1.Print amtEnd SubEnd Sub

Page 8: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Passing Subprograms: Passing Variables Variables

►Example 2: Result Example 2: Result

2 2 6 22 2 6 2

=> Variable was passed by value in Example 2=> Variable was passed by value in Example 2

Page 9: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Passing Subprograms: Passing Variables Variables

►Example 3: SubroutineExample 3: Subroutine

Sub Triple( ByVal num As Single)Sub Triple( ByVal num As Single)Rem Triples a numberRem Triples a numberPicture1.Print num;Picture1.Print num;Let num = num*3Let num = num*3Picture1.Print num;Picture1.Print num;End SubEnd Sub

Page 10: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Passing Subprograms: Passing Variables Variables

►Example 3: Program Example 3: Program

Sub Command1_Click( )Sub Command1_Click( )Dim amt As SingleDim amt As SinglePicture1.ClsPicture1.ClsLet amt=2Let amt=2Picture1.Print amt;Picture1.Print amt;Call Triple(amt)Call Triple(amt)Picture1.Print amtPicture1.Print amtEnd SubEnd Sub

Page 11: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Passing Subprograms: Passing Variables Variables

►Example 3: Result Example 3: Result

2 2 6 22 2 6 2

=> Variable was passed by value in Example 3=> Variable was passed by value in Example 3

By default: Variable passing by referenceBy default: Variable passing by reference To be precise you may use ByRef as in:To be precise you may use ByRef as in:

Sub Triple( ByRef num As Single)Sub Triple( ByRef num As Single)

Page 12: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Local Subprograms: Local Variables Variables

► Example 1Example 1Sub Command1_Click( )Sub Command1_Click( )Picture1.ClsPicture1.ClsCall ThreeCall ThreeCall ThreeCall ThreeEnd SubEnd Sub

Sub Three( )Sub Three( )Dim num As SingleDim num As SinglePicture1.Print num Picture1.Print num Let num=3Let num=3Picture1.Print numPicture1.Print numEnd SubEnd Sub

Page 13: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Local Subprograms: Local Variables Variables

► Example 1: ResultExample 1: Result

0 3 0 30 3 0 3

=> => The variable num is local to the The variable num is local to the Subprogram;Subprogram;

Lives only for the time the Subprogram is Lives only for the time the Subprogram is called;called;

Initialized each timeInitialized each time

Page 14: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Local Subprograms: Local Variables Variables

► Example 2Example 2

Sub Command1_Click( )Sub Command1_Click( ) Sub Trivial ( )Sub Trivial ( )Dim x As SingleDim x As Single Dim x As SingleDim x As SinglePicture1.ClsPicture1.Cls Picture1. Print x;Picture1. Print x;Let x = 2Let x = 2 Let x = 3Let x = 3Picture1.Print x;Picture1.Print x; Picture1.Print x;Picture1.Print x;Call TrivialCall Trivial End SubEnd SubPicture1.Print x;Picture1.Print x;Call TrivialCall TrivialPicture1.Print x;Picture1.Print x;End SubEnd Sub

Page 15: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Local Subprograms: Local Variables Variables

► Example 1: ResultExample 1: Result

2 0 3 2 0 3 22 0 3 2 0 3 2

=> => The variable x has separate identities in the The variable x has separate identities in the Event Event procedure and in the subroutineprocedure and in the subroutine

Page 16: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Form-Level Subprograms: Form-Level Variables Variables

► Example 1Example 1Dim num1 As Single, num2 As SingleDim num1 As Single, num2 As Single

Sub Command1_Click( )Sub Command1_Click( ) Sub AddAndIncrement ( Sub AddAndIncrement ( ))Let num1 =2Let num1 =2 Picture1.Print Picture1.Print num1+num2num1+num2Let num2 =3Let num2 =3 Let num1= num1+1Let num1= num1+1Picture1.ClsPicture1.Cls Let num2=num2+1Let num2=num2+1Call AddAndIncrementCall AddAndIncrement End SubEnd SubPicture1.PrintPicture1.PrintPicture1.Print num1Picture1.Print num1Picture1.Print num2Picture1.Print num2End SubEnd Sub

Page 17: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

► Example 1: ResultExample 1: Result

553344

=> => The variable num1 and num2 known to allThe variable num1 and num2 known to all subprograms subprograms

Subprograms: Form-Level Subprograms: Form-Level Variables Variables

Page 18: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Subprograms: Form-Level Subprograms: Form-Level Variables Variables

► Example 2Example 2

Dim pi As SingleDim pi As Single

Sub Form_Load( )Sub Form_Load( )Let pi = 3.14159Let pi = 3.14159 End SubEnd Sub

Sub Command1_Click ( )Sub Command1_Click ( )Picture1.ClsPicture1.ClsPicture1.Print “Circle area is:” pi*5^2Picture1.Print “Circle area is:” pi*5^2End Sub End Sub

Page 19: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

► Example 2: ResultExample 2: Result

Circle area is: 78.53975Circle area is: 78.53975

=> => The variable pi is initialized in Form_load The variable pi is initialized in Form_load eventevent

Subprograms: Form-Level Subprograms: Form-Level Variables Variables

Page 20: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Functions Functions

Page 21: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Functions Functions ►General Format : SubroutineGeneral Format : Subroutine

Sub Sub SubprogrammeNameSubprogrammeName ( (list of parameterslist of parameters))statementsstatementsEnd SubEnd Sub

►General Format : FunctionsGeneral Format : FunctionsSub Sub FunctionNameFunctionName ( (list of parameterslist of parameters) As ) As datatypedatatypestatementsstatementsFunctionName = expressionFunctionName = expressionEnd FunctionEnd Function

Page 22: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Functions Functions ►Examples:Examples:

Function FtoC (t As Single) As SingleFunction FtoC (t As Single) As SingleFtoC = (5/9)*(t-32) FtoC = (5/9)*(t-32) End FunctionEnd Function

Function FirstName$ (nom As String)Function FirstName$ (nom As String)Dim firstSpace As IntegerDim firstSpace As IntegerRem Extract the first name from the full name nomRem Extract the first name from the full name nomLet firstSpace = Instr(nom, “ ”)Let firstSpace = Instr(nom, “ ”)FirstName$ = Left$(nom, firstSpace-1)FirstName$ = Left$(nom, firstSpace-1)End FunctionEnd Function

Page 23: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Functions Functions ►Using function in a programUsing function in a program

Sub Command1_ClickSub Command1_ClickDim x As SingleDim x As SinglePicture1.Print FtoC(212)Picture1.Print FtoC(212)x = FtoC(32)x = FtoC(32)Picture1.Print xPicture1.Print xx = FtoC(81+32)x = FtoC(81+32)Picture1.Print xPicture1.Print xEnd SubEnd Sub

Page 24: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Functions Functions (Contd.)(Contd.) ►Result:Result:

100100

00

4545

Page 25: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

► Practice ProblemPractice ProblemFunction Cider (g As Single, x As Single) As SingleFunction Cider (g As Single, x As Single) As SingleCider=g*xCider=g*xEnd FunctionEnd Function

Sub DisplayNumOfGallons(galPerBu, apples As Single)Sub DisplayNumOfGallons(galPerBu, apples As Single)Picture1.ClsPicture1.ClsPicture1.Print “You can make”; Cider(galPerBu, apples);Picture1.Print “You can make”; Cider(galPerBu, apples);Picture1.Print “gallons of cider.”Picture1.Print “gallons of cider.”End SubEnd Sub

Sub GetData (gallonsPerBushel As Single, apples As Sub GetData (gallonsPerBushel As Single, apples As Single)Single)

Let gallonsPerBushel =3Let gallonsPerBushel =3Let apples =9Let apples =9End SubEnd Sub

Functions Functions (Contd.)(Contd.)

Page 26: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Functions Functions (Contd.)(Contd.) ►Practice ProblemPractice Problem

Sub Command1_ClickSub Command1_ClickRem How many gallons of apple cider can we Rem How many gallons of apple cider can we make make Dim gallonsPerBushel As Single, apples as SingleDim gallonsPerBushel As Single, apples as SingleCall GetData(gallonPerBushel, apples)Call GetData(gallonPerBushel, apples)Call DisplayNumOfGallons(gallonsPerBushel, Call DisplayNumOfGallons(gallonsPerBushel, apples) apples) End SubEnd Sub

Page 27: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Functions Functions (Contd.)(Contd.) ►Practice Problem: ResultPractice Problem: Result

You can make 27 gallons of cider You can make 27 gallons of cider

Page 28: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Modular DesignModular Design

►Top-Down DesignTop-Down Design ►Structured ProgrammingStructured Programming

SequencesSequences DecisionsDecisions LoopsLoops

Page 29: Introduction to Computing Dr. Nadeem A Khan. Lecture 18.

Note:Note:

►Read: Schneider (Section 4.2,4.3, 4.4)Read: Schneider (Section 4.2,4.3, 4.4)►Read comments and do practice Read comments and do practice

problemsproblems of these sectionsof these sections►Attempt questions of ExercisesAttempt questions of Exercises


Recommended