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,

Slides:



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

Sub and Function Procedures
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
Procedural programming in Java
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
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
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Chapter 4 - Visual Basic Schneider
Chapter 4 (cont) Sec. 4.1, 4.2, 4.4 Procedures (User-defined)
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.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
Apply Sub Procedures/Methods and User Defined Functions
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,
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.
IE 212: Computational Methods for Industrial Engineering
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
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
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
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.
Procedural programming in Java Methods, parameters and return values.
Applications Development
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
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.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Lecture 7 Methods (functions and subroutines) Parameter Passing
Sub Procedures And Functions
IS 350 Application Structure
Functions and Procedures
Method.
Chapter 4 - Visual Basic Schneider
5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
Procedures and Functions
Introduction to Visual Programming
Chapter#8 General Procedures
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Procedures: Functions and Subroutines
Methods.
Chapter 8 - Functions and Functionality
Chapter 4 General Procedures
Introduction to VB programming
Presentation transcript:

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, but inconvenient for large ones –Much better to divide program into manageable pieces Benefits of using multiple procedures –Avoids repeat code (reuse a procedures many times in one program) –Promotes software reuse (reuse a procedures in another program) – Promotes debugging (can test an individual module to make sure it works properly) Two types of procedure to look at today: –Sub –Function

Sub Procedures The purpose of a Sub procedure is to operate and manipulate data within some specific context A procedure is invoked by using its defined name –For example: Message() You’ve been using Sub Procedures all the time: E.g. Console.WriteLine(“Hello World”)

Creating a Sub Procedure Type a procedure declaration into the Code window –Sub procedure-name() Visual Basic will create the procedure stub Type the required code

Exchanging Data with a Procedure Syntax for calling a Sub procedure into action: procedure-name(argument list) Calling a Sub Procedure

writeLine(“Hello World”) In the code above what is the subroutine name? How many arguments are in the argument list?

Exchanging Data with a Procedure (continued) A Sub procedure declaration must include: –Keyword Sub –Name of the general procedure The rules for naming Sub procedures are the same as the rules for naming variables –Names of any parameters Parameter: the procedure’s declaration of what data it will accept Argument: the data sent to the procedure Individual data types of each argument and its corresponding parameter must be the same

writeLine(“Hello World”) How would you describe this procedure’s parameter? What data type is it? What is the argument in this example?

Exchanging Data with a Procedure (continued) The Structure of a Sub Procedure Required Last Line Required Brackets Required Keyword The Procedure’s Name Optional Parameter List

Passing Parameters You can send items to a Sub procedure Sum(2, 3) Sub Sum(num1 As Double, num2 As Double) Console.WriteLine(num1+num2) End Sub In the Sum Sub procedure, 2 will be stored in num1 and 3 will be stored in num2 and the sum will be output to the console The order of the parameters determines which value is sent in as what variable! The data types must match!

Passing Variables We can pass variables too: x = 2 y = 3 Sum(x,y)‘ Same as Sum(2, 3) The variables are evaluated prior to calling the subroutine, and their values are accessible via the corresponding variable names in the sub

Population Density Sub Subroutine to calculate population density: Public Sub CalculateDensity(ByVal state As String, _ ByVal pop As Double, _ ByVal area As Double) Dim rawDensity, density As Double rawDensity = pop / area density = Math.Round(rawDensity, 1) ' Round to 1 decimal place Console.Write("The density of " & state & " is " & density) Console.WriteLine(" people per square mile.") End Sub VB.NET adds “ByVal” if you leave it off. We’ll discuss what this means shortly…

Parameters and Arguments CalculateDensity("Alaska", , ) Arguments – what you send to a Sub procedure Parameters – place holders for what the sub procedure receives Public Sub CalculateDensity(ByVal state As String, _ ByVal pop As Double, _ ByVal area As Double) If ByVal left off, VB.NET will add it

Code Reuse By making CalculateDensity a procedure subroutine, we can reuse it, e.g.: CalculateDensity(“Hawaii”, , 6471)

Passing by Value ByVal stands for “By Value” –Default mode, VB.NET adds this for you if you leave it off ByVal parameters retain their original value after Sub procedure terminates –Can think of this as a copy of the variable is sent in Public Sub ValSub(ByVal x As Integer) Dim x As Integer = 3 ValSub(x) Memory X 3

ByVal Example Public Sub CallingSub() Dim x As Integer x = 5 Console.WriteLine(“x is " & x) ValSub(x) Console.WriteLine(“x is " & x) End Sub Public Sub ValSub(ByVal x As Integer) x = 10 Console.WriteLine("x is " & x) End Sub Output?

ByVal Example – Y to X Public Sub CallingSub() Dim y As Integer y = 5 Console.WriteLine("y is " & y) ValSub(y) Console.WriteLine("y is " & y) End Sub Public Sub ValSub(ByVal x As Integer) x = 10 Console.WriteLine(" x is " & x) End Sub Output?

Passing by Reference ByRef stands for "By Reference“ –You can think of this as a reference, or pointer, to the original variable is sent to the subroutine ByRef parameters can be changed by the Sub procedure and retain the new value after the Sub procedure terminates Public Sub RefSub(ByRef x As Integer) Dim x As Integer = 3 RefSub(x) Memory X 3 X

ByRef Example Sub CallingSub() Dim x As Integer x = 5 Console.WriteLine(“x is " & x) RefSub(x) Console.WriteLine(“x is " & x) End Sub Sub RefSub(ByRef x As Integer) x = 10 Console.WriteLine("x is " & x) End Sub Any Difference in Output?

ByRef Example – Y to X Public Sub CallingSub() Dim y As Integer y = 5 Console.WriteLine("y is " & y) RefSub(y) Console.WriteLine("y is " & y) End Sub Public Sub RefSub(ByRef x As Integer) x = 10 Console.WriteLine(" x is " & x) End Sub Output?

Local Variables Variables declared inside a Sub procedure with a Dim statement Parameters are also considered local variables; their values are gone when the subroutine exits (unless parameters were passed ByRef)