Lecture 7 Methods (functions and subroutines) Parameter Passing

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 6- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Driving Test 1 Marking Scheme Focus on five areas to pass driving test 1.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
Chapter 4 General Procedures
CSI 101 Elements of Computing Spring 2009 Lecture #10 – Functions and Subroutines Monday, March 16th.
Example 2.
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.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Understanding class definitions Looking inside classes.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Apply Sub Procedures/Methods and User Defined Functions
Subroutines and Functions Chapter 6. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Why to Create a Procedure
CS0004: Introduction to Programming Subprocedures and Modular Design.
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.
Subroutines and Functions. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but inconvenient.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
Procedural programming in Java Methods, parameters and return values.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
ME 142 Engineering Computation I Using Subroutines Effectively.
Visual Basic Programming I 56:150 Information System Design.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
1 Microsoft® Visual Basic®.NET Language # 2. 2 Flow-Control Statements If … End If Select Case … End Select For… Next Do … Loop Exit.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Review Expressions and operators Iteration – while-loop – for-loop.
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.
JavaScript Modularity. Goals By the end of this lecture, you should … Understand why programmers use modularity. Understand how to create a function in.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
Dani Vainstein1 VBScript Session 5. Dani Vainstein2 What we learn last session? Branching Branching using If … Then … Else statement. Branching using.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Sub Procedures and Functions Visual Basic. Sub Procedures Slide 2 of 26 Topic & Structure of the lesson Introduction to Modular Design Concepts Write.
CS0004: Introduction to Programming
Sub Procedures And Functions
Functions and Procedures
Object-Oriented Programming: Classes and Objects
CS 106A, Lecture 7 Parameters and Return
Group Status Project Status.
CHAPTER 6 GENERAL-PURPOSE METHODS
VBScript Session 7 Dani Vainstein.
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Visual Programming
Tonga Institute of Higher Education
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
Procedures: Functions and Subroutines
Methods.
The structure of programming
Chapter 8 - Functions and Functionality
STARTING OUT WITH Visual Basic 2008
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Scope Rules.
Presentation transcript:

Lecture 7 Methods (functions and subroutines) Parameter Passing Scope of Variables ByVal or ByRef

Agenda: To understand the need for methods To know how to write and call methods To know how to pass parameters to a method ByRef vs. ByVal for parameters passing Scope of variables

What is a method? A method is self-contained block of code that performs some operations Methods break the program up and make it more understandable Methods promote code reuse DRY – Don’t Repeat Yourself

Why use methods? Problem You want to have same message box titles throughout the application.

If rbnLess21.Checked Then MessageBox.Show("Sorry,…", "Cruel Games") ElseIf rbn21to35.Checked Then MessageBox.Show("Let’s rock", "Cruel Games") Else MessageBox.Show("You…", "Cruel Games") End If

But what if you want to change the title of the message boxes?

You should create method Private Sub ShowMessage (ByVal text As String) MessageBox.Show(text, "Cruel Games") End Sub

Functions And what if you want to have consistency among your InputBoxes?

The code… Private Function GetInput(ByVal Message As Double) As String Return InputBox(text, “Marks") End Function

Sub and Function VB.NET can define a method using Sub keyword or using Function keyword Sub is used when the method does not return a value and is short for subroutine Function is used when the method does return the value

Calling a method You can call a method within a block in your program by specifying the name of function and parameters in parenthesis (or empty parenthesis) Dim mark As Integer = GetInput(“Enter the mark”) ShowMessage(“Let’s rock”)

ByVal vs ByRef

What will be printed? Sub Main() dim A as Integer A = 5 DoSmt(A) Debug.Write(A) ‘? End Sub Sub DoSmt(ByVal B as Integer) B = B + B

Sending by Value When we send the parameters to the function by value the called function creates a local copy of that variable and any changes to that variable will have no effect on the original variable. It is safe to send by value to avoid accidental change to the variable

The Answer is 5 … 5 5 10 Why? Main A DoSmt(By Val b As Integer)

Sending By Reference When parameter sent by reference the called function does not create a local copy of the original variable but it creates the reference to it. So changes applied to variable in the called function will effect the variable in the calling function. Used when we need to change the value of the variable in the function

What will be printed? Sub Main() dim A as Integer A = 5 DoSmt(A) Debug.Write(A) ‘? End Sub Sub DoSmt(ByRef B as Integer) B = B + B

The Answer is 10 Why? Main() … 5 A DoSmt(By Ref B As Integer) B

Scope

Variable Scope You can't define a variable at any spot in the program and use it at any spot, there is a specific relationship between where a variable is defined and where it can be used. This is known as the scope of the variable.

Variable Scope Block begins with an opening statement (For, If, Sub…) and ends with a closing one (usually Next, End If, End Sub…) Block defines a scope Variable declared inside the outer block will be visible in the inner block, but not vise-versa.

Example Dim y As Integer = 56 For i As Integer = 0 To 9 y = y – 1 ‘possible - y is visible in the inner block Next If (y < 3) Then Dim x As Integer = 9 End If x = 8 ‘impossible - x is defined in the inner block and is not accessible in the outer block

Variable Lifetime Variables are valid only after their declaration Variables are destroyed when their scope is finished Variables declared within a block will be destroyed when their block finishes execution

Example Dim y As Integer = 56 For i As Integer = 0 To 9 ‘i is created here y = y – 1 Next ‘i is destroyed at this point If (y < 3) Then Dim x As Integer = 9 ‘x is created here MsgBox("X = " & x) End If ‘x is destroyed at this point

The End