Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Computing Dr. Nadeem A Khan. Lecture 18."— Presentation transcript:

1 Introduction to Computing Dr. Nadeem A Khan

2 Lecture 18

3 Subprograms: Passing Variables ► Example 1: Subroutine Sub Triple( num As Single) Rem Triples a number Picture1.Print num; Let num = num*3 Picture1.Print num; End Sub

4 Subprograms: Passing Variables ► Example 1: Program Sub Command1_Click( ) Dim amt As Single Picture1.Cls Let amt=2 Picture1.Print amt; Call Triple(amt) Picture1.Print amt End Sub

5 Subprograms: Passing Variables ► Example 1: Result 2 2 6 6 => Variable was passed by reference in Example 1

6 Variable Passing by Val!

7 Subprograms: Passing Variables ► Example 2: Program Sub Command1_Click( ) Dim amt As Single Picture1.Cls Let amt=2 Picture1.Print amt; Call Triple((amt))‘extra parentheses Picture1.Print amt End Sub

8 Subprograms: Passing Variables ► Example 2: Result 2 2 6 2 => Variable was passed by value in Example 2

9 Subprograms: Passing Variables ► Example 3: Subroutine Sub Triple( ByVal num As Single) Rem Triples a number Picture1.Print num; Let num = num*3 Picture1.Print num; End Sub

10 Subprograms: Passing Variables ► Example 3: Program Sub Command1_Click( ) Dim amt As Single Picture1.Cls Let amt=2 Picture1.Print amt; Call Triple(amt) Picture1.Print amt End Sub

11 Subprograms: Passing Variables ► Example 3: Result 2 2 6 2 => Variable was passed by value in Example 3 By default: Variable passing by reference By 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)

12 Subprograms: Local Variables ► Example 1 Sub Command1_Click( ) Picture1.Cls Call Three End Sub Sub Three( ) Dim num As Single Picture1.Print num Let num=3 Picture1.Print num End Sub

13 Subprograms: Local Variables ► Example 1: Result 0 3 0 3 => The variable num is local to the Subprogram; Lives only for the time the Subprogram is called; Initialized each time

14 Subprograms: Local Variables ► Example 2 Sub Command1_Click( )Sub Trivial ( ) Dim x As SingleDim x As Single Picture1.ClsPicture1. Print x; Let x = 2Let x = 3 Picture1.Print x;Picture1.Print x; Call TrivialEnd Sub Picture1.Print x; Call Trivial Picture1.Print x; End Sub

15 Subprograms: Local Variables ► Example 1: Result 2 0 3 2 0 3 2 => The variable x has separate identities in the Event procedure and in the subroutine

16 Subprograms: Form-Level Variables ► Example 1 Dim num1 As Single, num2 As Single Sub Command1_Click( )Sub AddAndIncrement ( ) Let num1 =2Picture1.Print num1+num2 Let num2 =3Let num1= num1+1 Picture1.ClsLet num2=num2+1 Call AddAndIncrementEnd Sub Picture1.Print Picture1.Print num1 Picture1.Print num2 End Sub

17 ► Example 1: Result 534 => The variable num1 and num2 known to all subprograms subprograms Subprograms: Form-Level Variables

18 ► Example 2 Dim pi As Single Sub Form_Load( ) Let pi = 3.14159 End Sub Sub Command1_Click ( ) Picture1.Cls Picture1.Print “Circle area is:” pi*5^2 End Sub

19 ► Example 2: Result Circle area is: 78.53975 => The variable pi is initialized in Form_load event Subprograms: Form-Level Variables

20 Functions

21 Functions ► General Format : Subroutine Sub SubprogrammeName (list of parameters) statements End Sub ► General Format : Functions Sub FunctionName (list of parameters) As datatype statements FunctionName = expression End Function

22 Functions ► Examples: Function FtoC (t As Single) As Single FtoC = (5/9)*(t-32) End Function Function FirstName$ (nom As String) Dim firstSpace As Integer Rem Extract the first name from the full name nom Let firstSpace = Instr(nom, “ ”) FirstName$ = Left$(nom, firstSpace-1) End Function

23 Functions ► Using function in a program Sub Command1_Click Dim x As Single Picture1.Print FtoC(212) x = FtoC(32) Picture1.Print x x = FtoC(81+32) Picture1.Print x End Sub

24 Functions (Contd.) ► Result: 100 100045

25 ► Practice Problem Function Cider (g As Single, x As Single) As Single Cider=g*x End Function Sub DisplayNumOfGallons(galPerBu, apples As Single) Picture1.Cls Picture1.Print “You can make”; Cider(galPerBu, apples); Picture1.Print “gallons of cider.” End Sub Sub GetData (gallonsPerBushel As Single, apples As Single) Let gallonsPerBushel =3 Let apples =9 End Sub Functions (Contd.)

26 ► Practice Problem Sub Command1_Click Rem How many gallons of apple cider can we make Dim gallonsPerBushel As Single, apples as Single Call GetData(gallonPerBushel, apples) Call DisplayNumOfGallons(gallonsPerBushel, apples) End Sub

27 Functions (Contd.) ► Practice Problem: Result You can make 27 gallons of cider

28 Modular Design ► Top-Down Design ► Structured Programming  Sequences  Decisions  Loops

29 Note: ► Read: Schneider (Section 4.2,4.3, 4.4) ► Read comments and do practice problems of these sections of these sections ► Attempt questions of Exercises


Download ppt "Introduction to Computing Dr. Nadeem A Khan. Lecture 18."

Similar presentations


Ads by Google