1 / 19

Introduction to Computing

Introduction to Computing. Dr. Nadeem A Khan. Lecture 23. Subprograms /Subroutines revisited. Subprograms. General Format Sub SubprogrammeName ( list of parameters ) statements End Sub. Subprograms (Contd.). Examples: Sub ExplainPurpose ( )

warner
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 23

  3. Subprograms /Subroutinesrevisited

  4. Subprograms • General Format Sub SubprogrammeName (list of parameters) statements End Sub

  5. Subprograms (Contd.) • Examples: Sub ExplainPurpose () Rem Explain the task performed by the program Picture1.Print “This program displays sentences” Picture1.Print “identifying pair of numbers and their sums.” End Sub Sub Add ( num1 As Single, num2 As Single) Rem Display numbers and their sum • Picture1.Print “Sum of”; num1; “and”; num2; “is”; num1+num2 End Sub

  6. Subprograms (Contd.) • Using subprograms in a Program Sub Command1_Click( ) Rem Display the sum of several pairs of numbers Picture1.Cls Call ExplainPurpose Picture1.Print Call Add(2, 3) Call Add(4, 6) Call Add(7, 8) End Sub

  7. Subprograms (Contd.) • Local variables • Form-level variables

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

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

  10. Subprograms: Passing Variables • Example 1: Result 2 2 6 6 => Variable was passed by reference in Example 1

  11. Functions

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

  13. Functions • Example: Function FtoC (t As Single) As Single FtoC = (5/9)*(t-32) End Function

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

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

  16. Functions • Another Example: 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 Write a program which input the full name and output the first name to the user using this function!

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

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

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

More Related