# 1# 1 Using Procedures and Functions What is a procedure? What is a sub procedure (subroutine)? Built-in functions in your code What is a function? CS.

Slides:



Advertisements
Similar presentations
Integrated Business Applications with Databases (D3) Jenny Pedler
Advertisements

“Going loopy with Visual Basic!”
ISOM3230 Business Applications Programming
Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Tutorial 12: Enhancing Excel with Visual Basic for Applications
Programming in Visual Basic
VBA Modules, Functions, Variables, and Constants
Sub Programs To Solve a Problem, First Make It Simpler.
SUNY Morrisville-Norwich Campus-Week 12 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
Modeling using VBA. Using Toolbox This is a label This is a button Using the Toolbox select a GUI element and by mouse-click place it on the frame This.
CS241 PASCAL I - Control Structures1 PASCAL I - Control Structures Philip Fees CS241.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Python quick start guide
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
# 1# 1 VBA Objects, Message Boxes as Functions What is an object in VBA? How do you move between design mode and run mode? How can you make a cell become.
Tutorial 11 Using and Writing Visual Basic for Applications Code
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA 1 Lecture Outline Record macro and examine VBA code VBA Editor (IDE)
Lists in Python.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Copyright © 2008 Pearson Prentice Hall. All rights reserved. 1 Microsoft Office Excel Copyright © 2008 Pearson Prentice Hall. All rights reserved
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.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
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.
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
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.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Introduction to VB.NET 2005 Dr. McDaniel IDS4704 Spring 2005.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
7 1 User-Defined Functions CGI/Perl Programming By Diane Zak.
MIS 216 Exam1 Review Spring What to expect Questions like those on the home works and on the quizzes Evaluate code Create code Multiple choice and.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
259 Lecture 11 Spring 2013 Advanced Excel Topics – Loops.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
ME 142 Engineering Computation I Using Subroutines Effectively.
Macro’s Within excel. Most functionality can be driven from VBA VBA is the programming language that runs inside of excel. It uses visual basic as the.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
CompMathBSc, English 5 October 2006 Programming basics — continued  Arrays  Cycle Statements: Loops  Control Structures vs Conditions  Subs: Procedures.
ME 142 Engineering Computation I Using Subroutines Effectively.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Introduction to Files in VB Chapter 9.1, 9.3. Overview u Data Files  random access  sequential u Working with sequential files  open, read, write,
Unit 10-JavaScript Functions Instructor: Brent Presley.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Controlling Program Flow with Decision Structures.
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 15: Sub Procedures and Function Procedures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University.
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,
VBA - Excel VBA is Visual Basic for Applications
Spreadsheet-Based Decision Support Systems
User-Defined Functions
Using local variable without initialization is an error.
Exploring Microsoft Office Access 2007
Chapter 6 Sub Procedures
Introduction to Computer Programming IT-104
Presentation transcript:

# 1# 1 Using Procedures and Functions What is a procedure? What is a sub procedure (subroutine)? Built-in functions in your code What is a function? CS 105 Spring 2010

# 2# 2 Procedures A procedure is a block of programming code. There are two kinds of procedures: 1.Subroutines (sub procedures) 2.Functions If we have to repeat code, we isolate it in a subroutine or function in order to avoid programming or typographical errors and make our code easier to debug or change—you only have to change it in one place!

# 3# 3 CS 105 Spring 2010 Calling Sub Procedures We can invoke or call each sub procedure when we wish to perform those operations. Just write the name of the sub procedure. Commission Format (must give name of work sheet or place the code is stored if the sub procedure is not in “Modules") Commission Format

# 4# 4 CS 105 Spring 2010 Private Sub versus Public Sub Private Sub means the procedure can only be called from other procedures in its object (like a UserForm, or worksheet). Sub means available to all worksheets, can be called from any procedure in any sheet. Sheet1.Commission Commission

# 5# 5 CS 105 Spring 2010 For Excel to use it, and for each spreadsheet to use it, where should the public function be? A public function in a module can be accessed from all worksheets.

# 6# 6 CS 105 Spring 2010 Functions and Subroutines The main difference: Functions do things and return a value. strNew=Reverse(Cells(1,1).Value) Sub(routine)s do things, but do not return values. MsgBox "Welcome",vbExclamation, "Hi” Format 2

# 7# 7 CS 105 Spring 2010 Excel has many built-in functions that are the same as VBA functions, and vice-versa

# 8# 8 CS 105 Spring 2010 VBA Functions Predefined: A. vntAnswer = MsgBox("Quit?", vbYesNo, "Leaving?") B. If IsNumeric(strName) = True Then A Function is just like a subroutine, except that it returns a value to a variable or cell (A.) or is evaluated on the fly as above (B.)

# 9# 9 Quick Quiz What do we mean by IsNumeric returns True or False “on the fly”? The Value T or F is used immediately and discarded. For example, the content of a page that is sent to you from a Web site can be varied "on the fly" based on what pages you looked at previously—the page is dynamic and will change B CS 105 Spring 2010

# 10 CS 105 Spring 2010 Functions The value returned by a function can be True, False, a value, vbYes, etc. You invoke (CALL) a Function by mentioning its name (typically as part of a larger expression), together with its argument(s). Sometimes a function doesn’t have arguments— you cannot tell for sure by looking at the statement calling the function, but you can tell by looking at how the function is written. Cells(1,1).Value =Now()

# 11 CS 105 Spring 2010 We can create our own functions Reverse(strWord) is at the heart of the function The function Reverse wants an argument, either a cell address or a word, and then it will reverse the letters in the word With functions such as Average, Count, etc. we need to feed them an argument

# 12 CS 105 Spring 2010 Defining a Function Function Useful(strName as String, intNumber as Integer) As String Do something here Useful = "a string of some sort" End Function Parameters Body NameType of Return Value

# 13 CS 105 Spring 2010 Parameter vs. Argument You *declare* a function/sub procedure using something like this: Function Reverse(strWord as String) As String - strWord is called a *parameter* of the function Reverse You *call* a function/sub procedure using something like the following: Cells(1,1).Value = Reverse(strEntry) strEntry (or the value in a cell) is called an *argument* that you *pass* to the function Reverse. Note: what you pass to the parameter can have a different name than the parameter has.

# 14 CS 105 Spring 2010 So… To connect the two, a function/sub procedure defines a parameter, and the calling code passes a value/variable as an argument to that parameter. You can think of the parameter as a parking place and the argument as an automobile. Just as different automobiles can park in the parking place at different times, the calling code can pass a different argument to the same parameter each time it calls the procedure.

# 15 CS 105 Spring 2010 Functions: Returning a Value Function FixFormat(strName as String, intNumber as Integer) As String FixFormat = strName & " " & intNumber End Function

# 16 Private Sub cmdNew_Click() Dim strWord As String Dim strNoun As String Dim intQuantity As Integer strNoun = Cells(1, 2).Value intQuantity = Cells(2, 4).Value strWord = FixFormat(strNoun, intQuantity) MsgBox "The result is " & strWord End Sub Function FixFormat(strName as String, intNumber as Integer) As String FixFormat = strName & " " & intNumber End Function Joe 78 strNoun intQuantity Joe 78

# 17 CS 105 Spring 2010 Calling Functions Dim strAnswer as String strAnswer = DoCalculation(17) … What is the name of the function?DoCalculation What type of value does it return? How many arguments does it have? What are the type(s) of argument(s)? Better be a String One Probably an Integer

# 18 CS 105 Spring 2010 To Summarize: What is a procedure? What is a sub procedure (subroutine)? Built-in functions in your code What is a function?