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

Slides:



Advertisements
Similar presentations
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Advertisements

Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Introduction to Computing Dr. Nadeem A Khan. Lecture 9.
Chapter 4 General Procedures
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.
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
Chapter 41 Sub Procedures, Part II Passing by Value Passing by Reference Local Variables Class-Level Variables Debugging.
Introduction to Computing Dr. Nadeem A Khan. Lecture 13.
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.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
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.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
1 INF110 Visual Basic Programming AUBG Fall semester 2009 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice Hall,
Chapter 5 - VB 2008 by Schneider1 Chapter 5 - General Procedures 5.1 Sub Procedures, Part I 5.2 Sub Procedures, Part II 5.3 Function Procedures 5.4 Modular.
Why to Create a Procedure
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
E0001 Computers in Engineering Procedures: subprograms and functions.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
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.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
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.
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.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
Lecture 7 Methods (functions and subroutines) Parameter Passing
Introduction to Computing Dr. Nadeem A Khan. Lecture 24.
Introduction to Computing Dr. Nadeem A Khan. Lecture 21.
Sub Procedures And Functions
Subprograms Functions Procedures.
Final Exam Review Part 4 - VBA
Chapter 4 - Visual Basic Schneider
Procedures and Functions
Sub Procedures and Functions
Chapter 7: Using Functions, Subs, and Modules
Procedures: Functions and Subroutines
Presentation transcript:

Introduction to Computing Dr. Nadeem A Khan

Lecture 18

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; Call Triple(amt) Picture1.Print amt End Sub

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

Variable Passing by Val!

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

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

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

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

Subprograms: Passing Variables ► Example 3: Result => 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)

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

Functions

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

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:

► 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

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

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