1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology

Slides:



Advertisements
Similar presentations
Chapter 6, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 6 Sub Procedures And Functions.
Advertisements

Introduction to C Programming
Sub and Function Procedures
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1.
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Functions l Control structures l every C++ program must have a function called main l program execution.
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.
Functions Modules in C++ are called functions and classes
Chapter 41 General Procedures Sub Procedures, Part I Sub Procedures, Part II Function Procedures.
VB .NET Programming Fundamentals
Apply Sub Procedures/Methods and User Defined Functions
Chapter 7 Functions.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
Why to Create a Procedure
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
CS0004: Introduction to Programming Subprocedures and Modular Design.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 7 Sub and Function Procedures.
Chapter 6 Sub Procedures
Chapter 9: Writing Procedures Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 Functions every C++ program must have a function called main program execution always begins with function main any other functions are subprograms and.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
CS346 Javascript -3 Module 3 JavaScript Variables.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
Applications Development
Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
1 Chapter 7 Functions Dale/Weems/Headington. 2 Chapter 7 Topics l Writing a Program Using Functional Decomposition l Writing a Void Function for a Task.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
PSU CS 106 Computing Fundamentals II VB Declarations HM 5/4/2008.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 6 Sub Procedures And Functions.
Copyright © 2014 Pearson Education, Inc. Chapter 6 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,
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lecture 7 Methods (functions and subroutines) Parameter Passing
Sub Procedures And Functions
Information and Computer Sciences University of Hawaii, Manoa
Chapter 7 User-Defined Methods.
Royal University of Phnom Penh
Chapter 3: Using Methods, Classes, and Objects
The Selection Structure
Method.
Chapter 3 Introduction to Classes, Objects Methods and Strings
6 Chapter Functions.
Procedures and Functions
CIS16 Application Development and Programming using Visual Basic.net
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Procedures: Functions and Subroutines
Methods.
Object Oriented Programming in java
STARTING OUT WITH Visual Basic 2008
Corresponds with Chapter 5
Presentation transcript:

1 Computer Programming Andres, Wen-Yuan Liao Department of Computer Science and Engineering De Lin Institute of Technology

2 Chapter 7 Classes and Methods

3 Chapter 7 Topics l Abstraction and encapsulation in OOD l Designing the public interface for a class l Designing a class constructor l Data lifetime l Declaring methods l Parameter passing l Collecting classes in a namespace

7.5 Declaring Methods l Method call l Method declaring: n Heading n Statement Sequence (chapter 2)

Two Parts of Method Declaration Private Function square(number As Integer) _ As Integer Return number * number End Function Heading Statement Sequence

6 MethodDeclaration Heading Statement Sequence Method Declaration Syntax

7 What is in a statement sequence? 'Method heading 0 or more statements here 'Method closing

8 Statement Sequence Statement... Statement Sequence Syntax

9 What is in a method heading? Private Function Inits() As String Modifiers Method type Identifier Parameter list Result Type

10 Heading Syntax Heading Modifiers MethodType Identifier ( Identifier As _ TypeName, Identifier As TypeName... As TypeName )

11 Heading Syntax (Cont.) l Modifiers n Public n Private l MethodType n Sub –A method that dose not return a value is called a Sub. n Function –A method that returns a value is called a Function. l Identifier n Method name

12 Parameter List l is the means used for a method to share information with the block containing the call. l Parameter declaration n Enclosed in parentheses n optional list n Accepted by the method n With commas separating them n Consists of –Identifier –Type name

13 Examples Sub doNothing() Public Sub skipValue() Function trimax(num1 As Integer, num2 As Integer, num3 As Integer) As Integer Private Function Square(number As Integer) As Integer Public Function cube(number As Double) As Double

14 Parameters and Arguments l The parameter list in the method heading provides one mechanism by which the method call and the method exchange information. l Parameter: n An identifier declared between the parameters in a method heading. l Argument: n An expression listed in the call to a method.

15 Parameter Passing l Arguments are matched to parameters by their relative position within the two lists. Public Sub someMethod (param1 As Integer, param2 As Double, param3 As String) someMethod( argmentA * 2, ardumentB, argumentC) l Parameter Passing: n The transfer of data between the argument and parameters in a method call.

16 'Heading Public Shared Function power (base As Integer, _ exponent As Integer) As Integer Parameter Passing (Cont.) number = power ( 2, 5 ) 'Call

Method Calls A method is called by using the name of the method followed by ( ) enclosing a list of arguments. Console.WriteLine("Done") a = Math.Max(y,2) value = Math.Sqrt(x) A method call temporarily transfers control to the called method to perform its task.

18 MethodName( Argument List ) The argument list is used to communicate values to the method by passing information. The argument list can contain 0, 1, or more arguments, separated by commas, depending on the method. In VB.NET, arguments should be used only for sending data into a method. Method Calls (Cont.)

19 Method Calls (Cont’) l A method call temporarily transfers control to the called method’s code. l When the method’s code has finished executing, control is transferred back to the calling block.

When a method is called, Temporary memory is set up ( for its primitive data type arguments and any local variables, and also for the method’s name if method is not a sub). Then the flow of control passes to the first statement in the method’s body. The called method’s body statements are executed until one of these occurs: Return statement (with or without a return value), or, The method (a sub) is closed. Then control goes back to where the method was called.

A VB.NET method can return l in its identifier at most 1 value of the type specified (called the result type) in its heading. l But, a sub method cannot return any value in its identifier.

22 Return Syntax ReturnStatement Return Expression

Questions l Why is a method used? To do a task (implement responsibilities). l Can one method call another method? Yes l Can a method even call itself? Yes, that is called recursion. It is very useful and requires special care in writing so that an infinite number of calls are not made.

24 Primitive and Reference Types letter title book Dim letter As Char Dim title As String Dim book As String letter = 'J'c title = “Programming with VB” book = title 2002 “Programming with VB” Memory Location ‘J’

Parameters in VB.NET l With primitive data types (such as Integer, Double, Boolean), the parameter receives a copy of the value of the argument. Operations on the parameter do not affect the argument. l With reference types (such as String and other classes) the parameter receives a copy of the address where the object is stored. Making changes in the fields of the object referred to by the parameter does affect the argument object. Doing so is poor programming practice. l You can modify an argument to be passed as a reference type by placing the keyword ByRef before the parameter

26 Passing a simple type as a parameter (Passing by Value) Double arg Double arg Double arg Double param Double param1 Argument Parameter Call During Method Execution Return Copy Param1 =

27 Passing a simple and reference type (Passing by Value and Reference) Double arg1 Addr Double arg2 “Visual Basic.NET” Memory Address Double param1 Addr Double param2 Argument Parameter Simple type Reference type Copy Param1 =

28 Examples Module Module1 Sub Main() Dim x, y, z As Integer x = 4 y = 6 sum1(x, y, z) Console.WriteLine("z = " & z) x = 2 y = 3 z = sum2(x, y) Console.WriteLine("z = " & z) Console.ReadLine() End Sub Sub sum1(ByVal a As Integer, ByVal b As Integer, ByRef c As Integer) c = a + b End Sub Function sum2(ByVal a As Integer, ByVal b As Integer) dim c as integer c = a + b return c End Function End Module 4 x 6 yz 4 a 6 bc 102 x 3 y 5 z 2 a 3 b 5 c

29 Examples Module Module1 Function SwapByRef(ByRef a As Integer, ByRef b As Integer) Dim t t = a a = b b = t End Function Sub Main() Dim x, y As Integer x = 12 : y = 56 SwapByRef(x, y) Console.WriteLine("x = " & x) Console.WriteLine("y = " & y) Console.ReadLine() End Sub End Module

30 Examples Module Module1 Sub SwapByVal(ByVal a As Integer, ByVal b As Integer) Dim t t = a a = b b = t End Sub Sub Main() Dim x, y As Integer x = 12 : y = 56 SwapByVal(x, y) Console.WriteLine("x = " & x) Console.WriteLine("y = " & y) Console.ReadLine() End Sub End Module

31 Methods for Comparing Strings Method Parameter Returns Operation Performed Name Type EqualsString Boolean CompareToString Integer Tests for equality of string contents. Returns 0 if equal, a positive integer if the string in the parameter comes before the string associated with the method and a negative integer if the parameter comes after it.

32 String methods Method Parameter Returns Operation Performed Name Type ToLower none String toUpper none String Returns a new identical string, except the characters are all lowercase. Returns a new identical string, except the characters are all uppercase.

33 Examples Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles MyBase.Load Dim A, B As Integer A = 10 : B = 15 Label1.Text = " 參考呼叫前 " & " A=" & A & " B=" & B & _ vbNewLine & vbNewLine CallByRef(A, B) Label1.Text += " 參考呼叫後 " & " A=" & A & " B=" & B End Sub Sub CallByRef(ByRef X As Integer, ByRef Y As Integer) X += 3 : Y += 2 Label1.Text += " 參考呼叫中 " & " X=" & X & " Y=" & Y & _ vbNewLine & vbNewLine End Sub

34 Examples Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles MyBase.Load Dim A, B As Integer A = 10 : B = 15 Label1.Text = " 傳值呼叫前 " & " A=" & A & " B=" & B & _ vbNewLine & vbNewLine CallByVal(A, B) Label1.Text += " 傳值呼叫後 " & " A=" & A & " B=" & B End Sub Sub CallByVal(ByVal X As Integer, ByVal Y As Integer) X += 3 : Y += 2 Label1.Text += " 傳值呼叫中 " & " X=" & X & " Y=" & Y & _ vbNewLine & vbNewLine End Sub

35 Examples Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim tot1, tot2 As Integer tot1 = Sum(1, 10) Label1.Text = "1 加到 10 的總合為 " & tot1 & _ vbNewLine & vbNewLine tot2 = Sum(5, 12) Label1.Text += "2 加到 12 的總合為 " & tot2 End Sub Function Sum(ByVal vStart As Integer, ByVal vEnd As Integer) As Integer Dim i, total As Integer For i = vStart To vEnd total += i Next Return total ' 此敘述可改寫成 Sum = total End Function

36 Examples Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Sum(1, 10) Sum(5, 12) End Sub Function Sum(ByVal vStart As Integer, ByVal vEnd As Integer) Dim i, total As Integer For i = vStart To vEnd total += i Next Label1.Text += vStart & " 加到 " & vEnd & _ " 的總合為 " & total & vbNewLine & vbNewLine End Function

37 Examples Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Sum(1, 10) Call Sum(5, 12) End Sub Sub Sum(ByVal vStart As Integer, ByVal vEnd As Integer) Dim i, total As Integer For i = vStart To vEnd total += i Next Label1.Text += vStart & " 加到 " & vEnd & _ " 的總合為 " & total & vbNewLine & vbNewLine End Sub