Procedures Subs and Functions. Procedures Before OOP, subroutines were the primary high-level way to organize a program. In OOP, this role has been taken.

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.
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Sub and Function Procedures
Excel and Visual Basic. Outline Data exchange between Excel and Visual Basic. Programming VB in Excel.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
1.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
VBA Modules, Functions, Variables, and Constants
Example 2.
Constructors. You got plenty of experience using constructors in the Marching Band program. A constructor is the subroutine which creates objects from.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Java Unit 9: Arrays Declaring and Processing Arrays.
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.
CS0004: Introduction to Programming Variables – Numbers.
IE 212: Computational Methods for Industrial Engineering
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Visual Basic I Programming
Why to Create a Procedure
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Class 3 Programming in Visual Basic. Class Objectives Learn about input/output Learn about strings Learn about subroutines Learn about arrays Learn about.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
CS0004: Introduction to Programming Variables – Strings.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Chapter 6 Sub Procedures
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
B065: PROGRAMMING Sub Procedures I. Starter  Identify the separate tasks to be performed in the programming task below (break it down into numbered sections).
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
ME 142 Engineering Computation I Using Subroutines Effectively.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
ME 142 Engineering Computation I Using Subroutines Effectively.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Programming with Microsoft Visual Basic th Edition
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Chapter 3 AS3 Programming. Introduction Algorithms + data structure =programs Why this formula relevant to application programs created in flash? The.
Controlling Program Flow with Decision Structures.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Copyright © 2014 Pearson Education, Inc. Chapter 6 Procedures and Functions.
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,
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Subprograms Functions Procedures.
Royal University of Phnom Penh
Introducing Instructions
Object-Oriented Programming: Classes and Objects
Functions Chapter 6-Part 2.
Functions CIS 40 – Introduction to Programming in Python
Method.
Object-Oriented Programming: Classes and Objects
Chapter 6 Sub Procedures
Procedures and Functions
PROGRAMMING Sub Procedures I.
CIS16 Application Development and Programming using Visual Basic.net
The structure of programming
Chapter 8 - Functions and Functionality
STARTING OUT WITH Visual Basic 2008
Presentation transcript:

Procedures Subs and Functions

Procedures Before OOP, subroutines were the primary high-level way to organize a program. In OOP, this role has been taken over by the Class. Nevertheless, at some point the organization stops and the work begins. The work in any program is done by the procedures. 2

Subs and Functions Visual Basic uses two types of procedures: Subroutines (Subs) and Functions. We could add properties as well, but a property is simply one sub and one function operating under the same name. The words “procedure” and “method” both mean a Sub or a function. 3

Sub or Function? As far as VB is concerned, the only difference between a Sub and a Function is that a Function returns a value, but a Sub does not. However, good programming (as required in this course) demands a stricter distinction: – A Function is the calculator: it performs only one role— returning a value (usually based on the input parameters). It should perform no other tasks that cause changes in the working of the program. – A Subroutine is the worker that gets things done. It may make use of functions or other subs in order to get its job done. A sub may cause any number of changes in the working of the program. 4

Parameters Both subs and functions can take parameters, also known as arguments. In the sub below, the graphics object “g” is the only parameter. Parameters are by default ByVal (by value); VB will fill this in for you if you don’t type it. The other way of declaring a parameter, ByRef (by reference) is rarely used in VB; we won’t use it in

Parameters Subs and functions can take multiple parameters of different types. Multiple parameters should be separated by commas. The parameters work just like local variables in the sub; whatever value is passed to a parameter is used wherever that parameter’s name is used in the procedure. 6

Parameter Example This sub has five parameters: two Strings, two Integers, and one Boolean. When the sub is called, whatever value is passed as the first parameter will become the value of hometeam, the second value will become visitors, etc. 7

Calling a Procedure The sub below calls the DisplayScore sub several times. The comments explain the various ways that a sub can be called. 8

Optional Parameters Parameters can be declared Optional. Optional parameters must come after all required parameters. They are declared using the keyword Optional before ByVal. They must have a default value which will be used if the calling code doesn’t include a value for this parameter. This is indicated by typing “ = DefaultValue” after the data type. The following slide shows an example: 9

Optional Parameters The optional parameter is Name. The default value is “Sir/Madam”. Here are two lines of code which call this sub: 10

Wait for the Movie If you are having trouble understanding how subs, functions, and parameters work, I have created another exciting* video which demonstrates them in action and in greater detail. The video is in Resources/Videos under the name “Subs, Functions, Debugging Video”. It should be playable on CAEN computers. It requires the Flash player. The VB program demonstrated in the video is also available there: it is called SubsFunctionsVideoExample.zip. If you think you understand subs and functions but want to see how the debugging tools work, you can jump to six minutes and ten seconds into the video. 11 * Yeah right.

Parameter Arrays Sometimes you will want to have a subroutine or function which takes an indefinite number of parameters. To do this, use a parameter array. The function below demonstrates: 12

Parameter Arrays When you have a procedure which includes a parameter array, you can call it just by passing a comma-separated list of values, like this: You can also pass it an array of the appropriate type, like this: 13

Functions A function is like a sub—it is a procedure that can take parameters. The differences are: – A function returns a value; – A function’s only function (so to speak) is to return a value. 14

Functions—One purpose only! Suppose that you had written a square root function like this: If those three subroutines do what they promise, you’re going to be unhappily surprised if you ever use this function! 15

What functions should and shouldn’t do Functions should ONLY figure out the return value. They should NEVER change the value of variables or properties, and they shouldn’t call subroutines. NO SIDE EFFECTS! They can call other functions and use variables defined in the class. 16

Bad Function Example 1 This function is bad because it assigns a value to Label1.Text. It won’t work if there is no Label1. Changing Label1’s Text property is an unanticipated side effect. 17

Bad Function Example 2 The line “MessageToSend = s” makes this a bad function. 18

Function Return Types Since the purpose of a function is to return a value, all functions should have data types. VB requires this if Option Strict and Option Explicit are both on (as required for this class). The data type can be – any built-in value type (Integer, Double, String, Boolean) – Any built-in reference type (Form, Label, etc.) – Any data type defined in the program: a class type, an enumeration type, a structure type, or an interface type. The return type is given after the close parenthesis of the parameter list, using “As” (As Integer, As BandMember, etc.) 19

Returning the Value VB.NET provides two ways to return a value from a function: 1.Use the function name, as in VBA: 2.Use Return. The book recommends this method. 20

Calling Functions Since a function returns a value of a particular datatype, it can be used in expressions just like variables of that data type: 21

Functions Video For a detailed explanation of how functions and subroutines are called and parameters are passed, Watch the SubsFunctions video available in Ctools. You’ll also learn a lot about using the powerful debugging tools in VB. 22