(Subroutines in Visual Basic)

Similar documents
Part 4 - Procedures and Functions

Function: function procedures and sub procedures share the same characteristics, with

Chapter 6 Sub Procedures

Level 3 Computing Year 2 Lecturer: Phil Smith

Sub Programs. To Solve a Problem, First Make It Simpler

Lab Sheet 4.doc. Visual Basic. Lab Sheet 4: Non Object-Oriented Programming Practice

VB Net Debugging (Console)

String sequence of characters string Unicode Characters immutable they cannot be changed after they have been created.

Language Fundamentals

MIS 216 SPRING 2018 PROJECT 4

Duhok Polytechnic University Amedi Technical Institute/ IT Dept. Halkawt Rajab Hussain

Chapter 2.5 Writing maintainable programs

Introduction to C# Applications

Programming with Visual Studio Higher (v. 2013)

Introduction to. Copyright HKTA Tang Hin Memorial Secondary School 2016

Software Development Techniques. 26 November Marking Scheme

Relational Operators. > greater than < less than >= greater than or equal to <= less than or equal to <> not equal to = equal to

Understanding the problem

MARK SCHEME for the October/November 2013 series 9691 COMPUTING. 9691/23 Paper 2 (Written Paper), maximum raw mark 75

Arrays. Storing Multiple Values

Programmers should write code that is self-documenting and split into small sections.

Darshan Institute of Engineering & Technology for Diploma Studies

VBA Handout. References, tutorials, books. Code basics. Conditional statements. Dim myvar As <Type >

Lecture 5 Tao Wang 1

CS130/230 Lecture 12 Advanced Forms and Visual Basic for Applications

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Introduction to Computer Science Midterm 3 Fall, Points

Outline. Example: Entertaining Friends. Modular Programming. Example: Entertaining Friends. Example: Entertaining Friends. Lecture 1.

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Cambridge Assessment International Education Cambridge International General Certificate of Secondary Education. Published

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

NOTES: Procedures (module 15)

Scheme of work Cambridge International AS & A Level Computing (9691)

Unit 4 Advanced Features of VB.Net

Lab # 02. Basic Elements of C++ _ Part1

Cambridge Assessment International Education Cambridge International Advanced Subsidiary and Advanced Level. Published

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Cambridge Assessment International Education Cambridge International Advanced Subsidiary and Advanced Level. Published

One Dimension Arrays 1

w w w. p a f c i t y. w o r d p r e s s. c o m

2.2 Data representation

OCR Pseudocode to Python

Advanced Computer Programming

Before We Begin. Introduction to Computer Use II. Overview (1): Winter 2006 (Section M) CSE 1530 Winter Bill Kapralos.

ISRA University Faculty of IT. Textbook BARBARA DOYLE C# Programming: From Problem Analysis to Program Design 4 th Edition

Procedural programs are ones in which instructions are executed in the order defined by the programmer.

Programmer-Defined Functions

Cambridge Assessment International Education Cambridge International Advanced Subsidiary and Advanced Level. Published

Excel VBA Variables, Data Types & Constant

Programming Studio #9 ECE 190

COMS 3101 Programming Languages: Perl. Lecture 2

Introduction to Computer Programming Using FORTRAN 77

Lesson 10 Using and Sorting Lists

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Unit: Rational Number Lesson 3.1: What is a Rational Number? Objectives: Students will compare and order rational numbers.

BASIC ELEMENTS OF A COMPUTER PROGRAM

General Certificate of Education Advanced Subsidiary Examination June 2010

Statements and Operators

20. VB Programming Fundamentals Variables and Procedures

TABLE OF CONTENTS 2 CHAPTER 1 3 CHAPTER 2 4 CHAPTER 3. Algorithm Design & Problem Solving. Programming. Databases

UNIVERSITY OF THE FREE STATE DEPARTMENT OF COMPUTER SCIENCE AND INFORMATICS CSIS1614

Introduction To C#.NET

Reusing this material

Variable A variable is a value that can change during the execution of a program.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Lecture 6. Statements

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

Cambridge Assessment International Education Cambridge International Advanced Subsidiary and Advanced Level. Published

Console Input / Output

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

We ve covered enough material so far that we can write very sophisticated programs. Let s cover a few more examples that use arrays.

Fundamentals of Computing and Digital Literacy. Sample. Marking Scheme

CSC 112 :: Test #2 Study Guide with Answers April 25, 2012 Short Answer Questions

AIS Cube [THE BLAZINGCORE SERIES] LANGUAGE REFERENCE

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

QUIZ: What value is stored in a after this

AIS Cube [THE BLAZINGCORE SERIES] LANGUAGE REFERENCE

A Crash Course on Limits (in class worksheet)

MARK SCHEME for the May/June 2011 question paper for the guidance of teachers 9691 COMPUTING. 9691/23 Paper 2 (Written Paper), maximum raw mark 75

COBOL 13. Language Summary. Version 0.01

May not be distributed except as part of a system distributed by a registered AltFileSearch licensee

Herefordshire College of Technology Centre Edexcel BTEC Level 3 Extended Diploma in Information Technology (Assignment 1 of 3)

EE 472 Embedded Systems. Name solutions. Instructions:

9691 COMPUTING. 9691/22 Paper 2 (Written Paper), maximum raw mark 75

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types

Microsoft Visual Basic 2005: Reloaded

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Heap sort. Carlos Moreno uwaterloo.ca EIT

Higher Computing Science Software Design and Development - Programming Summary Notes

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Software Development Techniques. December Sample Exam Marking Scheme

Lecture 23: Pointer Arithmetic

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

To become familiar with array manipulation, searching, and sorting.

Pointers, References and Arrays

Transcription:

Ch 7 Procedures (Subroutines in Visual Basic) Visual Basic Procedures Structured Programs To simplify writing complex programs, most Programmers (Designers/Developers) choose to split the problem into simpler smaller tasks (modules). They then solve each small task before linking the 'modules' together as a complete program. The modules, being smaller and simpler, are easier to design, write and test. The final program is made by combining individually tested modules, so that final checking consists of making sure that the modules work together as intended. Advantages to structuring a program in this way are that it aids readability when designing or amending. It also reduces the size of the program (a module is written once and can be used in several parts of a program and even included in other programs). Modules /Subroutines/Procedures A module is also called a subroutine or a procedure. All BASIC code for forms and control objects are contained in modules, but (as explained above) a complex task can be simplified by breaking it down into smaller steps and tackling each step as a separate module. A subroutine is created in BASIC as follows: (looks similar to a Form or object event procedure) SUB subname (parameter,...) END SUB : : : statements : : : subname parameter - the name of the subroutine. Used to call the subroutine from the main program. - one or more variables passed to the subroutine from the main program. ByVal The value of the variable is passed to the procedure. This mean that the value in the main program is not changed. ByRef The memory location is passed to the procedure. This means that the value in the main program IS changed. Ch7-Procedures WED Section 7-1

Example This is a trivial problem but serves to demonstrate the use of a subroutine within an event procedure Sub Main() Dim FirstNum As Integer Dim SecondNum As Integer Console.WriteLine("Test Max()") Console.Write("Enter First Number :") FirstNum = Console.ReadLine() Console.Write("Enter Second Number :") SecondNum = Console.ReadLine() Max (FirstNum, SecondNum) Console.ReadKey() The Main program Calls the procedure Max with the two numbers as the parameters. --------------------------------------------------------------- Sub Max(ByVal n1 As Integer, ByVal n2 As Integer) If N1 > n2 Then Console.WriteLine("{0} is greater.", n1) Else Console.WriteLine("{0} is greater.", n2) End If The procedure Receives FirstNum as n1 & SecondNum as n2 Program Run Test Max ( ) Enter First Number : 10 Enter Second Number : 5 10 is greater The program accepts two numbers entered from the user and sends them to the Subroutine Max() in its parameters. Max() then decides which number is greater and displays it on screen. Note that Max receives its parameters ByVal (see previous page) Also the received variables do not have to have the same name as their name in the main program. This is a simple example of a Procedure but that s how it works folks! Exercise 7.1 1) Create the program above & test it. 2) Add a new function called Min to display the lowest value. 3) What if the two values are equal? Ch7-Procedures WED Section 7-2

An Example using ByRef. In this example the parameters are passed ByRef. The procedure SWAP receives two variables in its parameter and swaps them into order (Highest & lowest). When the main program writes the variables the swap routine has put them into order. Sub Main() Dim FirstNum As Integer Dim SecondNum As Integer Console.WriteLine("Test Swap()") Console.Write("Enter First Number :") FirstNum = Console.ReadLine() Console.Write("Enter Second Number :") SecondNum = Console.ReadLine() Swap(FirstNum, SecondNum) Console.WriteLine("Highest value is {0}", FirstNum) Console.WriteLine("Lowest value is {0}", SecondNum) Console.ReadKey() ------------------------------------------ Sub Swap(ByRef n1 As Integer, ByRef n2 As Integer) Dim temp As Integer If n1 < n2 Then temp = n1 n1 = n2 n2 = temp End If The parameters are received as ByRef so the changes made to the variables will be taken back into the main program. Temp is a local variable Program Run: Test Swap() Enter First Number : 10 Enter Second Number : 5 Highest value is 10 Lowest value is 5 Exercise 7.2 1) Create the program above and test it 2) Change the data types to string and get the swap() procedure to sort into alphabetical order. Ch7-Procedures WED Section 7-3

Further Example As a further example consider the following procedure that determines the grade awarded to a student. The procedure has two parameters: Mark is declared as Integer to receive the students mark (ByVal) Grade is declared as Char to return the grade (ByRef) SUB CalcGrade (ByVal Mark as Integer, ByRef Grade as Char) IF Mark > 75 THEN Grade = "Dist" ELSEIF Mark > 60 THEN Grade = "Merit" ELSEIF Mark > 45 THEN Grade = "Pass" ELSEIF Mark > 35 THEN Grade = "Ref" ELSE Grade = "Fail" END SUB A series of IF...THEN statements test the student Mark and set the grade accordingly. When the procedure has completed the appropriate grade is returned through the parameter to the main program. The call instruction could take the form: CalcGrade (TotalMark, Grade) Exercise 7.3 1) Write a program that uses the CalcGrade procedure to determine the grade for a student. Ch7-Procedures WED Section 7-4

Global & Local Variables A Procedure can be thought of as a small program in its own right. It creates a memory block of its own and (as well as the parameters it receives) may create its own variables if needed. The variables declared (DIM) within a procedure are called local variables and are used in that procedure only (i.e. local to that procedure). Variables declared in the main program are called global variables and can be used within the main program and any procedure or function within the module. It is not advisable to change a global variable within a procedure or function unless the variable has been passed as a parameter using ByRef. Scope Scope defines the visibility of a variable. Variables can have scope ranging from global where any procedure in the application can access the variable to local used only in a single procedure. There are three types of scope for variables in Visual Basic: Global Scope -Global variables are in scope anywhere in your application. Module Scope -Module level variables are in scope anywhere within the module where they are declared. Local Scope -Local variables are only in scope within the procedure where they are declared. Variables may be declared as follows: Term Public Private Visibility Anywhere in or outside of a project Only in the block where defined Ch7-Procedures WED Section 7-5