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

Slides:



Advertisements
Similar presentations
True BASIC Ch. 9 Practice Questions. What are the 4 errors? DIM arr(4) FOR X = 1 TO 4 READ arr(X) LET arr(X) = arr(X) * X PRINT What is the new Y INPUT.
Advertisements

A Level Computing#BristolMet Session Objectives#U2 S8 MUST identify the difference between a procedure and a function SHOULD explain the need for parameters.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Introduction to Computing Dr. Nadeem A Khan. Lecture 10.
Introduction to Computing Dr. Nadeem A Khan. Lecture 23.
Introduction to Computing Dr. Nadeem A Khan. Lecture 22.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 10.
Introduction to Computing Dr. Nadeem A Khan. Lecture 27.
Chapter 4 - Visual Basic Schneider
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to VB prepared by Dr. I A Moneim Reference Book Schneider.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Introduction to Arrays Chapter 7 Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread.
Introduction to Computing Dr. Nadeem A Khan. Lecture
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 17.
Introduction to Computing Dr. Nadeem A Khan. Lecture 19.
Introduction to Computing Dr. Nadeem A Khan. Lecture 11.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 14.
Introduction to Computing Dr. Nadeem A Khan. Lecture 6.
Introduction to Computing Dr. Nadeem A Khan. Lecture 28.
Introduction to Computing Dr. Nadeem A Khan. Lecture 13.
Introduction to Computing Dr. Nadeem A Khan. Lecture 18.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Introduction to Computing Dr. Nadeem A Khan. Lecture 8.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
Arrays. What is an Array? Similar to a Matrix. Collection of data that needs similar processing. Example: Transpose of a matrix.
Introduction to Computing Dr. Nadeem A Khan. Lecture 5.
For…Next : Nested Loop X= 0 For i = 0 To 4 For j = 0 To i-1 X=X+(i+j-1) Next j Next i How many times the inner loop is executed? What is x at the end of.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
E0001 Computers in Engineering Procedures: subprograms and functions.
Sub procedures School of Business Eastern Illinois University © Abdou Illia, Spring 2002 (Week 6, Friday 2/21/03)
Subprograms CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Sub Procedures. A Sub procedure is a block of code that is executed in response to an event. There are two types of Sub procedures, general procedures.
Procedures and Modular Programming Part # 2. Interface Block ► Functions do not have to be internal to main program ► They can be stand-alone ► In this.
Count and add list of numbers From user input and from file.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Introduction to Computing Dr. Nadeem A Khan. Lecture 7.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Sub Procedures; Passing Values Back From Sub Procedures Passing by reference Passing by value.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
General Procedures Chapter 4. Different Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular Design (not.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Control Structures: Examples. for-loop example Q: If a=1, b=3, and x=7, what is the value of x when the loop terminates? A: x=1 for(k=a; k
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Sub Procedures And Functions
Visual Basic Fundamental Concepts
Starting Out with Java: From Control Structures through Objects
Chapter 4 - Visual Basic Schneider
Visual Basic..
Chapter 6 - Visual Basic Schneider
Visual Basic 6 Programming.
Chapter#8 General Procedures
Sub Procedures and Functions
Chapter 7: Using Functions, Subs, and Modules
Introduction to Value-Returning Functions: Generating Random Numbers
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
The structure of programming
Basic 9 Mr. Husch.
For...Next Statements.
Top-Down Design with Functions
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 21

Sub Command1_Click ( ) Sub Command1_Click ( ) Dim x As Integer, y As Integer, num As Integer Let x=2 Let y=3 Let num = Val(Text1.Text) Select Case num Case y-x, x Picture1.Print “Buckle my shoe.” Case Is<=4 Picture1.Print “Pick up sticks.” Case x+y To x*y Picture1.Print “Lay them straight.” Case Else Picture1.Print “Start all over again.” End Select End Sub

Read Chapter 5 completely!

Subprograms /Subroutines

Subprograms (Contd.) ► General Format Sub SubprogrammeName (list of parameters) statements End Sub

Subprograms (Contd.) ► Example 1 Sub ExplainPurpose ( ) Rem Explain the task performed by the program Picture1.Print “This program displays sentences” Picture1.Print “identifying pairs of numbers and their sums.” End Sub

Subprograms (Contd.) ► Example 2 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.) ► 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.) ► The Program’s result This program displays sentences identifying pair of numbers and their sums. Sum of 2 and 3 is 5 Sum of 4 and 6 is 10 Sum of 7 and 8 is 15

Subprograms (Contd.) ► Using subprograms in a Program Sub Command1_Click( ) Dim x As Single, y As Single, z As Single Picture1.Cls Let x = 2 Let y = 3 Call Add(x,y) Call Add(x+2, 2 * y) Let z = 7 Call Add(z, z+1) End Sub

Subprograms (Contd.) ► The Program’s result This program displays sentences identifying pair of numbers and their sums. Sum of 2 and 3 is 5 Sum of 4 and 6 is 10 Sum of 7 and 8 is 15

Subprograms (Contd.) ► Writing subprograms for a Program Sub Command_Click( ) Dim x As Single, y As Single, z As Single Picture1.Cls Call ComputeWage(“Aslam”, 5, 100) Call ComputeWage(“Saleem”, 6, 90) End Sub Result: Wage of Aslam is Rs. 500 Wage of Saleem is Rs. 540

Subprograms (Contd.) ► The Subprogram ComputeWage: Sub ComputeWage(Name As String, hourlyRate As Single, hours As Single) Rem Compute and displays employees wage Dim wage As Single wage = hourlyRate*hours Picture1. Print Name;” wage is”; wage End Sub

► Write a subroutine to reverses the input string Sub Command1_Click ( ) Dim nom As String Picture1.Clsnom=Text1.TextReverse(nom) End Sub Example: For Next Loop - SubProgram

Sub Reverse(info As String) Dim m As Integer, j As Integer, temp As String Let temp = “” For j = Len(info) To 1 Step -1 Let temp = temp + Mid$(info, j, 1) Next j Picture1.Print temp End Sub Example: For Next Loop - SubProgram

Local Variable and Form Level Variable

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

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

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

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

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

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

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

► Example 2: Result Circle area is: => The variable pi is initialized in Form_load event Subprograms: 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 => Variable was passed by reference in Example 1