190 likes | 314 Views
Explore the concepts of subprograms and functions in computing, with examples and practice problems to enhance your understanding. Learn how to pass variables, use local variables, and apply functions effectively in your programs.
E N D
Introduction to Computing Dr. Nadeem A Khan
Subprograms • General Format Sub SubprogrammeName (list of parameters) statements End Sub
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
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
Subprograms (Contd.) • Local variables • Form-level variables
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
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
Subprograms: Passing Variables • Example 1: Result 2 2 6 6 => Variable was passed by reference in Example 1
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
Functions • Example: Function FtoC (t As Single) As Single FtoC = (5/9)*(t-32) End Function
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
Functions (Contd.) • Result: 100 0 45
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!
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
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
Functions (Contd.) • Practice Problem: Result You can make 27 gallons of cider