Introduction to Visual Programming

Slides:



Advertisements
Similar presentations
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Advertisements

Sub and Function Procedures
Spring Semester 2013 Lecture 5
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Eight Sub and Function Procedures.
JavaScript Part for Repetition Statement for statement Cpecifies each of the items needed for counter-controlled repetition with a control variable.
1.
An Introduction to Programming with C++ Fifth Edition
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
VBA Modules, Functions, Variables, and Constants
Example 2.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Apply Sub Procedures/Methods and User Defined Functions
Why to Create a Procedure
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
Programming with Microsoft Visual Basic th Edition
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
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,
1 CS 106 Computing Fundamentals II Chapter 42 “Sub Procedures And Functions” Herbert G. Mayer, PSU CS Status 8/5/2013 Initial content copied verbatim from.
Lecture 7 Methods (functions and subroutines) Parameter Passing
CS0004: Introduction to Programming
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Subprograms Functions Procedures.
Chapter 9: Value-Returning Functions
User-Written Functions
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 7: User-Defined Functions II
IS 350 Application Structure
Object-Oriented Programming: Classes and Objects
COMP 170 – Introduction to Object Oriented Programming
Chapter 10: Void Functions
Revision Lecture
The Selection Structure
JavaScript: Functions.
Using Procedures and Exception Handling
Object-Oriented Programming: Classes and Objects
User-Defined Functions
Chapter 4 - Visual Basic Schneider
Chapter 6 Sub Procedures
Chapter 3 Simple Functions
Chapter 3 Introduction to Classes, Objects Methods and Strings
INPUT & OUTPUT scanf & printf.
Chapter 4 void Functions
Functions.
Procedures and Functions
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 9: Value-Returning Functions
Topics Introduction to Functions Defining and Calling a Function
Object Oriented Programming in java
Variables in C Topics Naming Variables Declaring Variables
Chapter 10: Void Functions
CPS125.
Introduction to Computer Programming IT-104
Presentation transcript:

Introduction to Visual Programming XIV Lecture 4: Methods Introduction to Visual Programming Lecture 4 – Methods XIV – Ian elcoate Ian Elcoate - May 2007

Methods by Structure Two basic types of structured methods Sub Method Can be used for any standalone block of code Header uses keyword: Sub Footer is always: End Sub Function Method Used when we know that the standalone block of code must calculate and return a result Header uses keyword: Function Footer is always: End Function Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Methods by Generation Event Method Intrinsic Method (Built-in methods) Created by double-clicking on a control at design-time E.g. btnExit_Click Structure: Usually a Sub Method (99.9%) Into whose body we write relevant code Intrinsic Method (Built-in methods) Already created and defined as part of VB.NET Structure: Can be either Sub or Function Methods We do not code them, but use them by calling E.g. CStr() User Defined Methods (UD) Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

User Defined Methods User Defined Methods (UD) Sub Methods XIV Lecture 4: Methods User Defined Methods User Defined Methods (UD) Are created, coded and called by programmer Structure: Can be either Sub or Function Methods Sub Methods Also known as Subs or Procedures Self contained block of code Function Methods Simply known as Functions Basically a Sub with a hole in it i.e. should always returns a result Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk Ian Elcoate - May 2007

User Defined Sub Method XIV Lecture 4: Methods User Defined Sub Method Format: Private Sub MethodName() ‘statements go here – in body End Sub Note: The MethodName should start in Uppercase Besides this, follow the variable naming rules / style If header is correctly typed, VBN will auto complete the footer for you, after pressing the Enter key Each word in Method name should start with a Uppercase letter Method name should not be same as a keyword or start with an number Where to declare method Avoid using same identifier already used, e.g. control or variable name. Type header and footer automatically follows Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk Ian Elcoate - May 2007

User defined Function Method XIV Lecture 4: Methods User defined Function Method Format: Private Function MethodName() As Return-Type ‘Optional Statements . . . Return Return-Value End Function Note: Inside the function some return-Value is always returned, using the keyword Return Thus the function header must specify the data-type of this Return-Value, i.e. the Return-Type e.g. As Integer If you type the header correctly, VBN will auto complete the footer for you, after pressing the Enter key Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk Ian Elcoate - May 2007

Sub and Function examples Sub Method Private Sub ShowCircleArea() lblArea.Text = CStr(Math.PI * 10 * 10) End Sub Function Method Private Function GetCircleArea() As Double Return (Math.PI * 10 * 10) Note the result of calculation is the return-value Thus return-type is specified as Double Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Calling Subs and Functions Both Subs and Functions are called by name A Sub Method call is a full statement in its own right ShowCircleArea() A Function Method call is an expression, i.e. it should be one part of a statement Usually the RHS of an assignment statement area = GetCircleArea() lblArea.Text = CStr(GetCircleArea()) When using functions, the LHS should be the same data-type as the RHS If not then remember to Convert the Function result Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Parameters Parameters are the means by which a method or function receives extra information Parameters can be considered to be valve in the top of the code-block The data will only be allowed to enter, if it satisfies certain conditions Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Parameter List Parameters use memory-space, thus need to be defined This is done by specifying a parameter list Inside the round brackets of a function or method header Format: Private Sub MethodName(paramList) Private Function FunctionName(paramList) As Return-Type The paramList is simply a list of parameters There can be none, one or more parameters If there are no parameters then the round brackets are left empty Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Defining Parameters Within the Parameter list, each parameter need to be formally defined On the most part this definition is very similar to variable declarations Except we do not use the Dim (or Private) Keywords Examples: Private Sub Display(pBalance As Double) Private Function Cube(pNum As Integer) As Integer Parameter names are prefixed with a lowercase p Besides this standard variable name rules apply By default VBN will precede each parameter with the ByVal keyword Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Arguments For any parameter specified by a method / function XIV Lecture 4: Methods Arguments For any parameter specified by a method / function The call must supply a corresponding argument Arguments are specified within the round brackets of the method / function calls The argument must be of same type as parameter Arguments must be provided in correct order Any value can be used as an argument Variables, e.g. result = Cube(num) Literal values, e.g. Display(7) Expressions, e.g. Cube(CInt(txtNum.Text)) Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk Ian Elcoate - May 2007

ByVal and ByRef Did you notice that we did not use the Dim keyword when declaring the parameter pRadius Instead parameters require either ByVal - Data Passed By Value ByRef - Data Passed By Reference If we use neither keyword then VBN assumes ByVal I.e. the parameter is assigned the value of the argument For a variable argument this means: The value stored in the variable argument will be passed to the parameter Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Passing By Reference (1) A ByRef parameter is not passed the (value of the) data stored by a variable argument Instead it is passed the memory address of the argument This is why ByRef arguments must be variables This means that the parameter and argument will both share the same memory address Note: Memory Addresses can only store one item of data Thus whatever is the value of the parameter at the end of the method Will also be the new value of the variable argument Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Passing By Reference (2) ByRef is a difficult concept to appreciate and as such is used in very specific circumstances When a method needs to return two or more results Customised validation Manipulating arrays For the time being lets consider the first option Functions return a result But only one value can be returned Making use of ByRef parameters allows for more than one value to be returned Note: to avoid confusion I will use ByRef parameters with Sub Methods only Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Example use of ByRef A method to calculate and return the area and circumference of a circle The radius is provided as a ByVal parameter To return two results the area and circumference are provided as ByRef parameters Private Sub CalcCircle(pRadius As Double, ByRef pArea as Double, ByRef pCir As Double) pArea = Math.PI * pRadius * pRadius pCir = 2 * Math.Pi * pRadius End Sub Remember in the call, variable arguments must be used to match up with the ByRef parameters CalcCircle(radius, area, circumference) Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk

Summary Modular programming creates faster and more efficient applications This requires blocks of code to be written as distinct methods We have already used methods (i.e. Intrinsic and Event methods) Now we will also use Sub Methods and Function Methods We pass variable data to methods by means of an argument within the method call To receive this data, matching parameters must be declared within header of method Lecture 4 "Methods" - Ian Elcoate - based on work by Asher Rashid - www.24x.co.uk