Subprocedures and Functions Visual Basic provides a number of facilities for creating modularized and reusable program code. The two you will learn about.

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

CS0004: Introduction to Programming Select Case Statements and Selection Input.
Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA By Robert T. Grauer Maryann Barber.
Example 2.
String Variables Visual Basic for Applications 4.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
5.05 Apply Looping Structures
Apply Sub Procedures/Methods and User Defined Functions
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Creating Embedded Formative Assessment Dr. Steve Broskoske Misericordia University EDU 533 Computer-based Education.
IE 212: Computational Methods for Industrial Engineering
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Chapter 12: How Long Can This Go On?
CS0004: Introduction to Programming Subprocedures and Modular Design.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
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.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter Two Creating a First Project in Visual Basic.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA.
Applications Development
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
ME 142 Engineering Computation I Using Subroutines Effectively.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
31/01/ Selection If selection construct.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
CSC 162 Visual Basic I Programming. String Functions LTrim( string ) –Removes leading spaces from the left side of string RTrim( string ) –Removes trailing.
Chapter 6 Controlling Program Flow with Looping Structures.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
Visual Basic Fundamental Concepts
A variable is a name for a value stored in memory.
UNIT 5 Lesson 15 Looping.
Access VBA Programming for Beginners - Class 3 -
Apply Procedures to Develop Message, Input, and Dialog Boxes
Spreadsheet-Based Decision Support Systems
Chapter 4: The Selection Structure
3rd prep. – 2nd Term MOE Book Questions.
Working with Forms in Visual Basic
CSI 101 Elements of Computing – Spring 2009
Chapter 4 - Visual Basic Schneider
Visual Basic..
Department Array in Visual Basic
Exploring Microsoft Excel
CIS16 Application Development Programming with Visual Basic
VBScript Session 6 Dani Vainstein.
Messages and Input boxes
CIS 16 Application Development Programming with Visual Basic
Control Structures Part B - Message and Input Boxes
Sub Procedures and Functions
Introduction to Programming
Değişkenler Variables can temporarily store the value of data in memory during the execution of your program. They are often referred to as containers.
Presentation transcript:

Subprocedures and Functions Visual Basic provides a number of facilities for creating modularized and reusable program code. The two you will learn about in this chapter are subprocedures and functions. Both subprocedures (you’ll notice the keyword Sub at the beginning and ending of program blocks) and functions are known as procedures, which let you organize large programmable problems into smaller programming pieces.

Subprocedures and Functions In Visual Basic, the distinction between subprocedures and functions is minimal: Like subprocedures, functions are called with their names; however, they return a value to the calling statement. Functions have data types just as variables do. These data types are assigned to the function name and are used to denote the data type of the returning value. Assigning a value to the function’s name is the resulting returned value. This returning value can be assigned to a variable or used in a larger expression.

Subprocedures and Functions In short, procedures are simply void functions. So what’s a function, and what’s a void function? Whether you know it or not, you have already been using functions in this book. Simply remember the intrinsic Visual Basic functions Left(), Right(), Mid(), UCase(), and InStr(). Each of these functions takes a parameter as input and returns a value as output. Subprocedures, on the other hand, return no output or, in other words, are void of output. Basically, you will want to use functions when you need a value return to a calling statement and subprocedures when no return value is needed.

Adding Subprocedures 1.Editör içerisinde doğrudan yazarak 2.Tools * Add Procedure... The basic syntax for a subprogram is as follows: Public Sub Procedure_ Name() ‘Your code goes in here End Sub

Calling Subprocedures (Kullanılması) Private Sub Form_ Load() Call MyProcedure MyProcedure End Sub

Alt Yordamlarda Parametre Kullanımı Public Sub Add_Two_Numbers( x As Integer, y As Integer) Dim liResult As Integer liResult = x + y End Sub Private Sub Form_ Load() Call Add_Two_Numbers( 5, 3) Add_Two_Numbers 5, 3 End Sub

Exiting Subprocedures Public Sub Add_ Two_ Numbers( x As Integer, y As Integer) Dim liResult As Integer If x < 0 Or y < 0 Then Exit Sub End If liResult = x + y End Sub

Functions Visual Basic has many built-in or intrinsic functions.

Syntax for creating a Visual Basic function Public Function Function_Name( variable1 as DataType, variable2 as DataType,...) As DataType ‘Your code goes here End Function I do not use the keyword Sub to denote a subprogram; instead, I use the keyword Function. Functions can take many parameters as depicted in the example. the function itself is assigned a data type.

Fonksiyon Örneği Public Function Add( operand1 as Integer, operand2 as Integer) As Integer Add = operand1 + operand2 End Function After the two integers are added together, they are assigned to the name of the function, in this case Add. After that, the value of the added integers assigned to the name of the function is returned back to the calling procedure. Private Sub Command1_ Click() Dim result as Integer result = Add( 5, 15) End Sub

Exiting Functions Like subprocedures, functions can be exited from anywhere in the function before executing all function statements. To accomplish this, use the keywords Exit Function.

Interacting with the user Message Box Input Box

Message Box Message boxes are perfect when you want to alert the user to something happening in your program. For example, an error has occurred, or a game has just ended. Maybe you want to display a congratulatory note or a question about retrying some action.

MsgBox “Message”, Buttons, “Title Bar Caption” Örnek: MsgBox “My Message Box”,, “Chapter 5”

VB Button and Icon Types

MsgBox ile değişken kullanımı Dim lsMessageString as String lsMessageString = “Visual Basic Programming for the Absolute Beginner” &_ “by Michael Vine, 2001” MsgBox lsMessageString,, “Chapter 5”

Input Box Sometimes message boxes do not provide enough interaction between your program and the user. For example, what if you want to prompt the user for his or her name? Maybe you want to prompt a user to enter a starting level before a game begins. You can implement these types of scenarios with the Visual Basic input box.

Input Box InputBox( Prompt, [Title], [Default], [XPos], [YPos], [HelpFile], [Context]) Örnek: InputBox (“What is your name?”, “My Input Box Example”, “Your name goes here”)

Input Box Toplanan verinin bir değişkene ya da bir özelliğe atanması lblOutput. Caption = “Welcome “ & InputBox(“ What is your name?”, “My Input Box Example”, “Your name goes here”) Sayıları alırken dikkat! Dim liNumber As Integer liNumber = Val( InputBox(“ Enter a number between 1 and 10”, “A number Question”))