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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Methods Java 5.1 A quick overview of methods
Procedural programming in Java
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008.
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.
Chapter 6: Functions.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Python quick start guide
Apply Sub Procedures/Methods and User Defined Functions
IE 212: Computational Methods for Industrial Engineering
1 VBA – podstawowe reguły języka Opracowanie Janusz Górczyński wg Microsoft Help.
Why to Create a Procedure
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
David Stotts Computer Science Department UNC Chapel Hill.
Procedural programming in Java Methods, parameters and return values.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
ME 142 Engineering Computation I Using Subroutines Effectively.
# 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.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Computer Science By: Erica Ligons Compound Statement A compound statement- block A compound statement- is a unit of code consisting of zero or more statement.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
CS0004: Introduction to Programming
Chapter 9: Value-Returning Functions
User-Written Functions
Lesson #6 Modular Programming and Functions.
Object-Oriented Programming: Classes and Objects
Lesson #6 Modular Programming and Functions.
Functions and Procedures
Method.
Programmazione I a.a. 2017/2018.
Object-Oriented Programming: Classes and Objects
User-Defined Functions
Lesson #6 Modular Programming and Functions.
Chapter 4 void Functions
Functions and Procedures
6 Chapter Functions.
VBScript Session 7 Dani Vainstein.
CS2011 Introduction to Programming I Methods (I)
Procedures Brent M. Dingle Texas A&M University
Tonga Institute of Higher Education
Lesson #6 Modular Programming and Functions.
Function.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Function.
CPS125.
Presentation transcript:

Sub Programs To Solve a Problem, First Make It Simpler

Top Down Design ä Top Down Design ä Start with overall goal. ä Break Goal into Sub Goals ä Break Sub Goals into Sub Sub Goals ä Until the Sub-Sub-Sub-Sub Goals are Easy.

Functions ä The VB Function is derived from a mathematical function: ä f(x) = x2 + 3x + 1 ä We use the function to calculate a single value.

Example Function ä A function called “factorial” takes in a number and returns its factorial. fact(5) returns 120 Factorial Function Number Factorial

How Functions are Used ä Functions are defined somewhere. ä They are then used in another place. ä The place they are used is called the Calling Function or Calling Sub Procedure or Calling Routine

Defining Our Own Functions ä Functions have several parts to them: ä The Header ä The Body ä The End Statement

Function Structure Header Body End Function even (num as integer) as Integer If((num mod 2) = 0) Then even = true Return End If even = false End Function

The Function Header ä The function header has: ä the word Function, ä the name of the function, ä the list of arguments it will accepts ä the function type Function Norb (fleeb as Integer) as Integer

Function Headers Must Have ä The Word Function at their beginning ä A Name - The names are restricted. ä An Argument List (possibly empty) ä What values will be sent in? ä A Type ä The function is going to return a value, what type will it be?

Function Bodies ä A series of statements. ä There is one statement that must be in that series, ä function_name = expression ä This indicates what value should be returned from the function.

Early Out ä The function body can contain an Exit Function statement. ä This returns control immediately to the calling function. ä Useful for Finding Something and Quitting

Function End Statement ä This contains the words End Function on a line by itself: ä End Function ä This is used to indicate the last statement in the Function Definition.

Example Function ä Two Numbers In, Smaller One Out Function Smaller (A as Integer, B as Integer) As Integer If A < B Then If A < B Then Smaller = A Smaller = A Else Else Smaller = B Smaller = B End If End If End Function

How do we get numbers in? ä What if we want to send a value in? ä We use an argument to receive values into the function. ä The arguments are used like variables and have what ever value was sent to them by the calling program. ä Arguments are described in the Header of the function.

Argument List of a Function ä The argument list will have the following form: ä ( ByVal var_name as type, ByVal next_var as type) ä Parts in italics are user defined, those in bold are optional, everything else is required.

Argument List ä Allows the function to operate on differing data. ä Variables followed by type (as in Dim) ä In Excel these are likely to be ranges.

Argument List ä The number of arguments are up to you. ä Each argument must have a type ä Each argument can be plain or ByVal ä Arguments are named just like variables ä ( Fleeb As Integer)

Using the Argument List ä Values are transferred to the arguments in order. ä The first value in the calling routine become the value of the first argument ä The second value becomes the second argument and so on.

x = 2 For i = 1 to 10 y = pow(i,x) y = pow(i,x) next i MsgBox CStr(y) Function Pow (m as integer, ex as single) as Single Pow = m ^ ex Pow = m ^ ex End Function Connecting the Function

Argument Examples Function Fleeb (R1 As Range) As Integer ä Takes in a range, returns an integer. Function Norb(R1 As Range, num As Integer) As Single ä Takes in a range and an integer, returns a single.

ByVal Modifier ä The optional ByVal modifier means that doing things to the argument in the function will have no effect on them in the calling function. ä This is good if we have several people writing a program and we want to make sure our function doesn’t interfere with what they are doing.

Argument Communication ä Plain arguments are two way communication. ä ByVal arguments are one way communications between the calling function and the function.

How ByVal Effects the Connection ä Before ByVal ä After ByVal

Side Effects ä Functions are not supposed to have any effect other than returning a value. ä If they do anything else it is known as a side effect. ä Side effects are usually not a good idea. ä Someone using your function has to be very, very careful so that your function doesn’t mess up their program.

Sub Procedures ä Sometimes we want to do something more than just pass back a single value. ä We can use a Sub Procedure to modify the value of several arguments. ä This is not a side effect with a Sub Procedure, it is what they are designed for.

Defining Sub Procedures ä Sub Procedures also have a header, body, and an end statement ä The header looks like this: SUB sub_name ( ByVal var_name as type,...) ä It doesn’t have a return type because it doesn’t “return” a value. ä Values are passed back through the argument list.

Calling Sub Procedures ä Unlike Functions, Sub Procedures are called using a special command. ä Call is used in front of the Sub: Call Sort_Array(Arg1,Arg2) ä This is done because the Sub is not evaluated to some value - it returns its value through the parameters in its argument list.

Sub Procedure Body ä The body of a Sub Procedure can have a series of statements ä There is no need to set the name of the Sub to a value, after all, we’re returning values via the argument list. ä A Exit Sub statement can be used to return control to the calling subprogram.

Sub Procedure End Statement ä The Sub Procedure is terminated by the statement End Sub.

Scoping ä Variables are only visible in a certain area - this area is known as their scope. ä The ability to encapsulate functionality in sub programs is enhanced by the limited scope of the variables in the sub- programs.

Scoping, Con’t ä Places where a variable can’t be seen, it can’t be modified. So we don’t have to worry about it. (Out of sight, out of mind...)

Scoping With Function/Sub ä Variables declared within a Sub or Function are only visible within them. Sub Fleeb () Dim a,b a = 0 End Sub Sub Norb () Dim a,b a = 3 End Sub 

Scoping Within a Module ä Variables declared at the module level are visible anywhere in the module. ä Module level variables are specified at the top of the page. ä Except where they are blocked by a local variable of the same name.

Illustration of Scoping Sub Fleeb () Dim a a = 0 ‘a,b,c,d are visible End Sub Sub Norb () Dim b b = 3 ‘b (the local one), c, d ‘are visible End Sub Module’s General Declaration Dim b,c,d

Modules in VB ä Modules (files containing just code) allow us to pass code between spreadsheets. ä Sometimes you want to give out your code in such a way that it can be added to an existing spreadsheet.

Global Variables ä Modules in a Spreadsheet can have variables accessible in two modules. ä This is done by declaring it “Global”, in which case it can be accessed from anywhere in the spreadsheet’s VBA code.

Scoping in a Project Module1 Global A,B Dim C,D Form1 Dim B,G Form2 Dim B,G,K Sub C Dim C,H Sub E () Dim B,A,L Function D Dim B,J Function F Dim L,M,J,E Sub A Dim A,E Function B Dim B,F

Variable Lifetime ä Variables only last as long as the code block they are in. ä After the code ends they evaporate. ä Unless: ä The values can be preserved by passing them back. ä The variables and their values can be preserved by declaring them static.

Summary ä Over the last few weeks we have seen how VB supports the decomposition of functionality. ä This included: ä the ability to declare a Sub or Function ä the ability to pass values from one Sub/Function to another, ä the scoping of variables, ä and the lifetime of variables.