PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008.

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 5 ( ) of Programming Languages by Ravi Sethi
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
VBA Modules, Functions, Variables, and Constants
Example 2.
Sub Programs To Solve a Problem, First Make It Simpler.
Chapter 4 - Visual Basic Schneider
Topics of Lecture Structural Model Procedures Functions Overloading.
PSU CS 106 Computing Fundamentals II VB Statements HM 5/19/2008.
6 April, 2000 CS1001 Lecture 15 EXAM1 - results SUBPROGRAM - - revisit FUNCTION.
6 April, 2000 CS1001 Lecture 13 PRACTICE EXAM - revisit SUBPROGRAM FUNCTION.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
CSC321: Programming Languages1 Programming Languages Tucker and Noonan Chapter 9: Functions 9.1 Basic Terminology 9.2 Function Call and Return 9.3 Parameters.
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:
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Apply Sub Procedures/Methods and User Defined Functions
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
IE 212: Computational Methods for Industrial Engineering
Why to Create a Procedure
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
1 CS 162 Introduction to Computer Science Chapter 4 Function Calls Herbert G. Mayer, PSU Status 11/9/2014.
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Functions.
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.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Some Advanced Features of Procedures. Recursion Recursive Calls –A procedure can call itself (Self Recursion) –A can call B, B calls C, etc, Z calls A.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
1 CS 163 Data Structures Chapter 6 Function Calls and Recursion Herbert G. Mayer, PSU Status 6/1/2015.
BACS 287 Programming Fundamentals 5. BACS 287 Programming Fundamentals This lecture introduces the following topics: – Procedures Built-in Functions User-defined.
Constructs for Data Organization and Program Control, Scope, Binding, and Parameter Passing. Expression Evaluation.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
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.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Information and Computer Sciences University of Hawaii, Manoa
Subprograms Functions Procedures.
Run-Time Environments Chapter 7
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Methods Chapter 6.
Principles of programming languages 4: Parameter passing, Scope rules
Organization of Programming Languages
Method.
Introduction to Visual Programming
Corresponds with Chapter 5
Presentation transcript:

PSU CS 106 Computing Fundamentals II VB Subprograms & Functions HM 4/29/2008

2 © Dr. Herbert G. Mayer Agenda Subroutine Syntax Call Statement Value Parameters Reference Parameter Function Syntax Order of Parameter Evaluation Recursion

3 © Dr. Herbert G. Mayer Subroutines Large VB programs can be modularized into smaller, logical entities: –subs and functions Subs (AKA Subroutines) as well as functions are defined with name and open a new, nested scope Both have parameters, are called, and scope can be constrained: –public or private –Default is private Subs invoked in a call statement Functions called by use in expressions –return a value to place of call Actual parameters must match defined formal parameters –one-to-one –by position –and by their respective types

4 © Dr. Herbert G. Mayer Sub Syntax Generic Syntax Rules: Scope Sub Name ( Formals ) Body End Sub Scopeoptional. If used, specify Private or Public Public extends visibility Nameany user-defined name, is callable by that name Formals0 or more formal parameters, comma-separated, if > 1 Each formalreference or value parameter, each with defined type Bodyoptional declarations, and optional statements A real example: Sub swap_if( ByRef a As Integer, ByRef b As Integer ) Dim Temp As Integer = a ‘ scope limited to: inside Sub If a < b Then a = b b = temp End If End Sub ‘ swap_if

5 © Dr. Herbert G. Mayer Call Statement Sub known as procedure, subroutine, proc in other languages Sub is called in call statement Use call statement any place where a statement is allowed Call transfers control to body of the named Sub –After binding the actual to the formal parameters Binding is done by position, left to right Body of callée is executed –until end is reached –or until a Return Statement is executed Return Statement is implied at end of Sub body Body may contain any number of local declarations Body may contain any number of statements, including 0 When Sub end is reached, or Return Statement executed, control transfers back to place after the call statement

6 © Dr. Herbert G. Mayer Parameter Passing At place of call, 0 or more actual parameters are provided, as defined in formal declaration Number of formal parameters is defined in the Sub Declaration Association of actual parameters to formal parameters is by position, from left to right Types must be pair-wise compatible If not equal, implied type conversion must ensure compatibility; else error If more than 1 parameter, each is separated from predecessor by comma

7 © Dr. Herbert G. Mayer Value Parameter Value Parameter specified via ByVal keyword in formal sub declaration Actual may be any type-compatible expression Actual parameter provides initial value, which is bound to the formal Formal may be assigned inside Sub during call But after return, the actual is unmodified, even if assignments to formal have taken place during call

8 © Dr. Herbert G. Mayer Value Parameter Local bound to Value Parameter unchanged after call Sub Demo_V( ByVal a As Integer ) Assert( a = 109, “Must be 109 in Demo_V, b4 is: ”, a ) a += 1 Assert( a = 110, “Must be 110 in Demo_V, aft is: ”, a ) End Sub ‘ Demo_V Sub Calling( ) Dim local As Integer = 109 Assert( local = 109, “Must be 109 in Calling b4”, local ) Demo_V( local ) Assert( local = 109, “Must be 109 in Calling aft”, local ) End Sub ‘ Calling

9 © Dr. Herbert G. Mayer Reference Parameter Reference Parameter specified via ByRef keyword in formal sub declaration Actual may be any type-compatible object, including a subscripted element or a full array Actual parameter may provide initial value, which then initializes the formal Formal may be assigned inside Sub during the call After return, actual parameter holds last value assigned to the formal during the call That is the major difference between Value and Reference Parameter passing!

10 © Dr. Herbert G. Mayer Reference Parameter Local bound to Reference Parameter changed after call Sub Demo_R( ByRef a As Integer ) Assert( a = 109, “Must be 109 in Demo_R, b4 is: ”, a ) a += 1 Assert( a = 110, “Must be 110 in Demo_R, aft is: ”, a ) End Sub ‘ Demo_R Sub Calling( ) Dim local As Integer = 109 Assert( local = 109, “Must be 109 in Calling b4”, local ) Demo_R( local ) Assert( local = 110, “Must be 110 in Calling aft”, local ) End Sub ‘ Calling

11 © Dr. Herbert G. Mayer Functions VB functions similar to Subroutines: –Function has name, by which it is called in expression –Function opens a new scope, like Sub –Function may have formal parameters, matched at call –Function returns a value –Return Type is specified in function declaration Function scope constrained as public or private Function invoked in expressions Function returns value to place of call Actual parameters must match defined formal parameters one-to-one by position and in type

12 © Dr. Herbert G. Mayer Function Syntax Generic Syntax Rules: Scope Function Name ( Formals ) As type Body Return expression ‘ anywhere in the body End Function Scopeoptional scope specified. If used: Private or Public Public extends visibility Nameany user-defined name, called by that name Typemust be any VB type; function returns value of that type Bodyoptional declarations, statement list with Return Statement A real example: Function max( ByVal a() As Integer, ByRef inx As Integer ) As Integer Dim max_val as integer = a(0) Dim i as integer inx = 0 For i = 1 To UBound ( a )‘ note built-in function UBound() If a(i) > max_val Then max_val = a(i)‘ found a new max inx = i‘ remember index of max End If Next Return max_val‘ return max, index passed by ref End Function

13 © Dr. Herbert G. Mayer Order of Parameter Evaluation For Subs and function calls, actual parameters are evaluated before starting call Before call each actual is bound to its corresponding formal parameter by position The types are equal, or else are converted, else are incompatible and cause an error Actual parameters are evaluated from left to right Note that this may have side-effects for subsequent actual parameter Actual parameter evaluation may even have side- effects for global objects, visible inside callée

14 © Dr. Herbert G. Mayer Recursion Def: An algorithm is recursive, if it is partly defined by simpler versions of itself Different from infinite regress, which is recursion gone awry! Sample of recursive algorithm, the factorial function of integer argument n, named fact(n) –fact(0)= 1 –fact(1)= 1 –for all n > 1: –fact(n)= fact(n-1) * n VBasic Sub may also be recursive, directly or indirectly Direct recursion: call to a() is issued from inside Sub for the same function a() Indirect recursion: call to a() is issued from another sub b(), that is called from a(), before a() returns

15 © Dr. Herbert G. Mayer Recursion Sample Function fibo( ByVal n As Integer) As Integer ‘ unsigned If n < 2 Then‘ base case, assumed unsigned Return n‘ return argument value Else Return fibo( n – 1 ) + fibo( n – 2 )‘ recurse! End If End Function ‘ fibo... Msgbox( “ Fibo( 5 ) = ” & fibo( 5 ) )