1 / 42

Introduction to Computing

Introduction to Computing. Dr. Nadeem A Khan. Lecture 19. Lecture 20 in Lab !. Functions. Functions. General Format : Subroutine Sub SubprogrammeName ( list of parameters ) statements End Sub General Format : Functions Sub FunctionName ( list of parameters ) As datatype

tiger
Download Presentation

Introduction to Computing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computing Dr. Nadeem A Khan

  2. Lecture 19

  3. Lecture 20 in Lab !

  4. Functions

  5. Functions • General Format : Subroutine Sub SubprogrammeName (list of parameters) statements End Sub • General Format : Functions Sub FunctionName (list of parameters) As datatype statements Function Name = expression End Function

  6. 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

  7. 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

  8. Functions (Contd.) • Result: 100 0 45

  9. Functions (Contd.) • 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

  10. Functions (Contd.) • 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

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

  12. Private and Public • Variables, Subroutines and Functions can be declared as Private or Public as well • Read Section 2.4 (Scott Warner) Scope • Local • Module/Form-Level • Global

  13. Modular Design • Top-Down Design • Structured Programming • Sequences • Decisions • Loops

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

  15. Conditions

  16. Conditions: Examples • Expression True/False 2 < 5 ? -5 > -2.5 ? 1 < 1 ? 1 = 1 ? 3.5 <= 3.5 ? -9 >= -35 ? -2 <> -3 ?

  17. Conditions: Examples (Contd.) • Expression True/False 2 < 5 True -5 > -2.5 False 1 < 1 False 1 = 1 True 3.5 <= 3.5 True -9 >= -35 True -2 <> -3 True

  18. Conditions: Examples (Contd.) • Expression True/False “cat” < “dog” ? “cart” < “cat” ? “cat” < “catalog” ? “9W” < “bat” ? “Dog” < “cat” ? “Sales-99 <= “Sales-retail” ?

  19. Conditions: Examples (Contd.) • For strings use the ANSI (or ASCI) table Characters ANSI(ASCI) Value Digits (0-9) 48-57 Upper Case (A-Z) 65-90 Lower Case (a-z) 97-122 • Compare two strings character by character

  20. Conditions: Examples (Contd.) • Expression True/False “cat” < “dog” True “cart” < “cat” True “cat” < “catalog” True “9W” < “bat” True “Dog” < “cat” True “Sales-99 <= “Sales-retail” True

  21. Conditions: Examples (Contd.) • Assume a= 4; b= 3; c=“hello”; d=“bye” • Expression True/False (a + b) < 2*a ? (Len(c) - b) = (a/2) ? c < “good” & d ?

  22. Conditions: Examples (Contd.) • Assume a= 4; b= 3; c=“hello”; d=“bye” • Expression True/False (a + b) < 2*a True (Len(c) - b) = (a/2) True c < “good” & d False

  23. Conditions (Contd.) • Condition is an expression involving relational operators; it is either True or False • Relational operators: =; <>;<;>;<=;>= => See Table 5.1

  24. Conditions (Contd.) • Complex Conditions: Conditions joined by Logical Operators • cond1 And cond2 • cond1 Or cond2 • Not cond1

  25. Conditions: Examples (Contd.) • Assume n= 4; answ=“Y” • Expression True/False (2<n)And(n<6) ? Not(n<6) ? (answ=“Y”) Or (answ=“y”) ?

  26. Conditions: Examples (Contd.) • Assume n= 4; answ=“Y” • Expression True/False (2<n)And(n<6) True Not(n<6) False (answ=“Y”) Or (answ=“y”) True

  27. Conditions (Contd.) • Operator hierarchy: In decreasing order of priority: • Arithemetic • Relational • Logical • Logical operator hierarchy: In decreasing order of priority: • Not • And • Or

  28. Conditions (Contd.) => - Tie: Left most operator is first - Use parantheses

  29. Decisions

  30. Decision Structure:Flowchart Is Condition True Process Step (s) 2 Process Step (s) 1

  31. IF BLOCKS IF condition Then action1 Else action 2 End If

  32. IF BLOCK (Contd.) • Complete the program: Identifies and displays the larger value and its variable (assume unequal values) Sub Command1_Click Dim a As Single, b As Single, largerVal As Single Let a=4 Let b=5 . . . Picture1. Print “Its value is:”;largerVal End Sub

  33. IF BLOCK (Contd.) Sub Command1_Click Dim a As Single, b As Single, largerVal As Single Let a=4 Let b=5 If a>b Then Picture1.Print “a has the larger value” largerVal=a Else Picture1.Print “b has the larger value” largerVal=b End If Picture1. Print “Its value is:”;largerVal End Sub

  34. IF BLOCK (Contd.) Result: b has the larger value Its value is: 5

  35. IF BLOCK (Contd.) What the following program will do?

  36. IF BLOCK (Contd.) Sub Command1_Click Dim a As Single, b As Single, largerVal As Single Let a=4 Let b=4 If (a>b) Or (a=b) Then Picture1.Print “a has the larger value” largerVal=a Else Picture1.Print “b has the larger value” largerVal=b End If Picture1. Print “Its value is:”;largerVal End Sub

  37. IF BLOCK (Contd.) Result: a has the larger value Its value is: 4

  38. IF BLOCK EXTENDED IF condition 1 Then action1 ElseIf condition 2 Then action 2 ElseIf condition 3 Then action 3 End If

  39. IF BLOCK EXTENDED(Contd.) What the following program will do?

  40. IF BLOCK EXTENDED(Contd.) Sub Command1_Click Dim a As Single, b As Single Let a=4 Let b=5 If (a>b) Then Picture1.Print “a has the larger value” ElseIf (a<b) Then Picture1.Print “b has the larger value” Else Picture1.Print “a and b have same value” End If End Sub

  41. IF BLOCK EXTENDED (Contd.) Result: b has the larger value

  42. Note: • Read: Schneider (Section 5.1, 5.2) • Read comments and do practice problems of these sections • Attempt questions of Exercises

More Related