1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Subprograms Functions Procedures. Subprograms A subprogram separates the performance of some task from the rest of the program. Benefits: “Divide and.
Sub and Function Procedures
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.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
Chapter 4 General Procedures
Example 2.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
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.
Chapter 4 - VB.Net by Schneider1 Chapter 4 General Procedures 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular.
Copyright © 2012 Pearson Education, Inc. Chapter 6 Modularizing Your Code with Methods.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
Python quick start guide
Apply Sub Procedures/Methods and User Defined Functions
Subroutines and Functions Chapter 6. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but.
Chapter 5 - VB 2008 by Schneider1 Chapter 5 - General Procedures 5.1 Sub Procedures, Part I 5.2 Sub Procedures, Part II 5.3 Function Procedures 5.4 Modular.
Why to Create a Procedure
1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
Dale Roberts Procedural Programming using Java Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Subroutines and Functions. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but inconvenient.
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Methods: A Deeper Look. Template for Class Definition public class { } A.Import Statement B.Class Comments C.Class Name D.Data members E.Methods (inc.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1Edited By Maysoon Al-Duwais. 2 General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Edited By Maysoon.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.
1Edited By Maysoon Al-Duwais. 2 Devices for Modularity Visual Basic has two ways for breaking problems into smaller pieces: Sub procedures Function procedures.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
1Lect7 GC20111/2/ General Procedures Function Procedure Sub Procedures, Part I Sub Procedures, Part II Modular Design Lect7 GC20111/2/2015.
CS0004: Introduction to Programming
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
Information and Computer Sciences University of Hawaii, Manoa
Method.
Chapter#8 General Procedures
6 Chapter Functions.
Chapter#8 General Procedures
Chapter 5 - General Procedures
Chapter 5 - General Procedures
Methods.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Standard Version of Starting Out with C++, 4th Edition
Corresponds with Chapter 5
Chapter 8 JavaScript: Control Statements, Part 2
Presentation transcript:

1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design

2 Devices for Modularity Visual Basic has two devices for breaking problems into smaller pieces: Function procedures Sub procedures

3 5.1 Function Procedures User-Defined Functions Having One Parameter User-Defined Functions Having Several Parameters User-Defined Functions Having No Parameters User-Defined Boolean-Valued Functions

4 Some Built-In Functions Function: Int Example: Int(2.6) is 2 Input: number Output: number Function: Math.Round Example: Math.Round(1.23, 1) is 1.2 Input: number, number Output: number

5 Some Built-In Functions (continued) Function: FormatPercent Example: FormatPercent(0.12, 2) is 12.00% Input: number, number Output: string Function: FormatNumber Example: FormatNumber(12.62, 1) is 12.6 Input: number, number Output: string

What is a Procedure? A Procedure is a collection of statements that are grouped together to perform a task. Function procedures return values. Sub Procedures do not. Procedure declaration:  Signature  Modifier(s) (optional) (e.g.public or private, static)  Function or Sub  Identifier  Formal Parameter List  Enclosed in parentheses  types and identifiers (like variable declaration)  Separated by commas  Return type (if a function)  Procedure Body  statements  requires return statement (if a function). Procedure call  Specify the identifier  Place actual parameters (arguments) in parentheses  Actual data (literal, constant, variable, or expression)  Use return value (if a function)

7 Function Procedures Function procedures (aka user-defined functions) always return one value Syntax: Function FunctionName(ByVal var1 As Type1, ByVal var2 As Type2,...) As ReturnDataType statement(s) Return expression End Function

Example With One Parameter Function FtoC(ByVal t As Double) As Double 'Convert Fahrenheit temp to Celsius Return (5 / 9) * (t - 32) End Function 8

Signature of the FtoC Function Procedure 9 What the author calls “header” is more widely known as signature. A function’s signature consists of a name, list of parameters, and return type. Subroutines do not have return types.

Header of the FtoC Function Procedure 10 Parameter declarations are like variable declarations. You give them a type. In addition, a parameter is either ByVal or ByRef. More about this later.

11 Example 5.1.1: Form txtTempF txtTempC

Example 5.1.1: Code 12

13 Example 5.1.1: Output

Example 5.1.1: Code 14 Function call Function declaration

Anatomy of Function Declaration and Call Function call return type identifier formal parameter(s) Function body return statement specify identifier Pass actual parameter(s) (arguments) use return value Function declaration

Processing Sequence of a Function Call Function call Function declaration 2) Pass by value: t is a copy of fahrenheitTemp. 3) Function body executes. 4) Return value sent back to calling statement (resolving the expression). 5) Return value assigned into celsiusTemp. 1) Call the function

Call Stack The.NET Framework keeps the local variables and parameters of a procedure in a Frame in the Call Stack. Frame = a data structure that holds the values of all the local variables and formal parameters of a procedure. Stack = a last-in, first-out data structure. Items are pushed onto the top of the stack. Items are popped off the top of the stack. When a procedure is called, its frame (local variables and parameters) are pushed onto the top of the stack. When a procedure terminates, its frame is removed from the stack.  the formal parameters and local variables of a procedure exist ONLY AS LONG AS THE PROCEDURE IS EXECUTING.

Using the Debugger to See Data in the Call Stack Two debugging windows: Call Stack – shows each item in the stack. This gives you a trace of procedure calls. Locals – shows all the local data (local variables and formal parameters) of the selected Procedure on the stack 18

19 Call Stack – shows only btnConvert_Click procedure Locals – shows local variables and parameters of btnconvert_Click Processing has paused here…right before the call to FToC.

20 Call Stack – shows that btnConvert_Click has called FToC. Locals – shows local variables and parameters of FToC Processing has paused here…inside FToC.

21 Call Stack –FToC has been popped. It’s local variables no longer exist, Locals – shows local variables and parameters of btnConvert_Click Processing has returned to btnConvert_Click from FToC.

Example With One String Parameter Function FirstName(ByVal fullName As String) As String 'Extract first name from full name Dim firstSpace As Integer firstSpace = fullName.IndexOf( " " ) Return fullName.Substring(0, firstSpace) End Function 22

23 Example 5.1.2: Form txtFullName txtFirstName

Example 5.1.2: Code Private Sub btnDetermine_Click(...) _ Handles btnDetermine.Click Dim fullName As String fullName = txtFullName.Text txtFirstName.Text = FirstName(fullName) End Sub Function FirstName(ByVal fullName As String) _ As String Dim firstSpace As Integer firstSpace = name.IndexOf( " " ) Return name.Substring(0, firstSpace) End Function 24

25 Example 5.1.2: Output

26 User-Defined Function Having Several Parameters Function Pay(ByVal wage As Double, ByVal hrs As Double) As Double Dim amt As Double 'amount of salary Select Case hrs Case Is <= 40 amt = wage * hrs Case Is > 40 amt = wage * 40 +(1.5 * wage * (hrs – 40)) End Select Return amt End Function

27 Example 5.1.3: Form txtWage txtHours txtEarnings

Example 5.1.3: Partial Code Private Sub btnCalculate_Click(...) _ Handles btnCalculate.Click Dim hourlyWage, hoursWorkded As Double hourlyWage = CDbl(txtWage.Text) hoursWorked = CDbl(txtHours.Text) txtEarnings.Text = FormatCurrency(Pay(hourlyWage, hoursWorked)) End Sub 28 Function call Remember the rule with parentheses…processing order goes from inside to out outside.

29 Example 5.1.3: Output

Example

Scope A variable’s scope is its visibility (which statements can use the variable). Variables declared in the class but outside any procedure have class scope. They can be used by any statement of any procedure within the class. Local variables and formal parameters have procedure scope. They can only be used inside the procedure in which they are declared. Variables declared inside blocks have block scope. They can be used only inside the block for which they are declared. Loop counter variables declared in the FOR statement of a FOR NEXT loop have loop scope; they can only be used within the loop for which they are declared. You can declare multiple variables of the same name as long as they are not in the same block structure. You cannot declare variables of the same name within the same block structure.

Duration A variable’s duration refers to how long (or when) the variable exists during processing. Goes hand in hand with scope Procedure scope variables usually exist only as long as the procedure executes (unless they are Static). Block scope exist only as long as the block executes. Loop counter variables declared in the FOR statement of a FOR NEXT throughout all repetitions of the loop, and then disappear. Class scope variables (for a form) last as long as the form is loaded.

Example Class Scope data.. Shared by all procedures in the class Class scope data is NOT in the call stack. It is in the HEAP, and remains in existence as long as the form is loaded.

Example Local variables.. Only available within the procedure Procedure scope data is in the call stack. It remains in existence only as long as the procedure is executing.

Example

36 User-Defined Function Having No Parameters Function CostOfItem() As Double Dim price As Double = CDbl(txtPrice.Text) Dim quantity As Integer = CDbl(txtQuantity.Text) Dim cost = price * quantity Return cost End Function

Function IsVowelWord(ByVal word As String) As Boolean If word.IndexOf("A") = -1 Then Return False End If. If word.IndexOf("U") = -1 Then Return False End If Return True End Function 37 Example 5.1.6