Methods.

Slides:



Advertisements
Similar presentations
Sub and Function Procedures
Advertisements

Pemrograman VisualMinggu …6… Page 1 MINGGU Ke Enam Pemrograman Visual Pokok Bahasan: Module, Class & Methods Tujuan Instruksional Khusus: Mahasiswa dapat.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Introduction to Methods
Review of C++ Programming Part II Sheng-Fang Huang.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Fund Raiser Application Introducing Scope, Pass-by-Reference and Option Strict.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 6: User-Defined Functions
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Chapter 3 (B) 3.5 – 3.7.  Variables declared in a function definition’s body are known as local variables and can be used only from the line of their.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
CSCI 3327 Visual Basic Chapter 6: More Examples of Methods UTPA – Fall 2011.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Programming Fundamentals Enumerations and Functions.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
Object-Oriented Programming: Classes and Objects.
 Pearson Education, Inc. All rights reserved Methods: A Deeper Look.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Object-Oriented Programming: Classes and Objects
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
by Tony Gaddis and Godfrey Muganda
Programming Fundamentals Lecture #7 Functions
Method.
Object-Oriented Programming: Classes and Objects
Lecture Set 4 Data Types and Variables
C++ for Engineers and Scientists Second Edition
Chapter 6 Methods: A Deeper Look
Chapter 5 - Functions Outline 5.1 Introduction
Chapter Topics Chapter 5 discusses the following main topics:
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Subroutines Web Programming.
Starting Out with Java: From Control Structures through Objects
Chapter 6 Methods: A Deeper Look
Chapter 4 void Functions
Lecture 22 Inheritance Richard Gesick.
6 Chapter Functions.
Introduction to Classes and Objects
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look
Tonga Institute of Higher Education
Chapter 9: Value-Returning Functions
Chapter 6 – Methods Topics are:
Starting Out with Java: From Control Structures through Objects
6 Methods: A Deeper Look.
Topics Introduction to Functions Defining and Calling a Function
Object Oriented Programming in java
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Chapter 5: Methods Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Java Methods: A Deeper Look Academic 2019 Class: BIT23/BCS10 Chapter 06 Abdulaziz Yasin Nageye Faculty of Computing Java How to Program, 10/e 1 © Co py.
6 Functions.
More C++ Classes Systems Programming.
Classes and Objects Systems Programming.
Presentation transcript:

Methods

Implicit Conversions Visual Basic converts an argument/operand values automatically in many instances. Widening and narrowing conversions are possible. Widening conversion occurs when a value is converted to a parameter of another type that can hold more data Narrowing conversion occurs when there’s potential for data loss during the conversion (that is, a conversion to a parameter of a type that holds a smaller amount of data).

Implicit Conversions Byte --> Byte, Short, Integer, Long, Decimal, Single, Double Short --> Short, Integer, Long, Decimal, Single, Double Integer --> Integer, Long, Decimal, Single, Double Long --> Long, Decimal, Single, Double Decimal --> Decimal, Single, Double Single --> Single, Double Double --> Double

Implicit Conversions Conversions also occur for expressions containing values of two or more types. In such expressions, the values’ original types are maintained, while temporary copies of the values are converted for use in the expression. Each value is converted to the “widest” type in the expression (that is, widening conversions are made until the values are of the same type as the “widest” type). For example, if doubleValue is of type Double and integerValue is of type Integer, when the expression doubleValue + integerValue is evaluated, the value of integerValue is converted to type Double (the widest type in the expression), then added to doubleValue, producing a Double result.

Option Strict and Data-Type Conversions Option Explicit Option Explicit—which is set to On by default requires you to declare all variables before they’re used in a program. This eliminates various errors. For example, when Option Explicit is set to Off, the compiler interprets misspelled variable names as new variables and implicitly declares them to be of type Object.

Option Strict and Data-Type Conversions A second option, which defaults to Off, is Option Strict. This option increases program clarity and reduces debugging time. When set to On, Option Strict causes the compiler to check all conversions and requires you to perform explicit conversions—that is, using a cast operator or a method to force a conversion to occur—for all narrowing conversions or conversions that might cause program termination (for example, conversion of a String, such as "hello", to type Integer).

Option Strict and Data-Type Conversions Class Convert Class Convert’s methods explicitly convert data from one primitive type to another. The name of each conversion method is the word To, typically followed by the name of the type to which the method converts its argument. For instance, to convert the String stored in numberTextBox.Text to type Double, we use the statement Dim number As Double = Convert.ToDouble(numberTextBox.Text) If numberTextBox does not contain a String that can be converted to a Double value, a FormatExcepton will occur.

Classes and Methods Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces—a technique known as divide and conquer. Typical programs consist of one or more classes. These classes are composed of smaller pieces called methods, instance variables and properties.

Classes and Methods There are several motivations for dividing code into methods. Divide-and-conquer approach makes program development more manageable. Software reusability—the ability to use existing methods as building blocks for new programs. Avoid repeating code in a program—when code is packaged as a method, the code can be executed from various points in a program simply by calling the method. This also makes the code easier to modify and debug.

Subroutines: Methods That Do Not Return a Value Our programs have contained at least one event-handler method that's called in response to an event and performs the program’s tasks. An event-handling method is a subroutine—a method that performs a task but does not return a value. A function is a method that performs a task then does return a value to the calling method.

Subroutines: Methods That Do Not Return a Value Subroutine Declarations The format of a subroutine declaration is Sub method-name(parameter-list) declarations and statements End Sub Keyword Sub indicates that this method will perform a task but will not return (that is, give back) any information to its calling method when it completes its task.

Subroutines: Methods That Do Not Return a Value Parameters and the Parameter List The parentheses after the method name are used to indicate any parameters—additional information that's required by the method to perform its task. An empty set of parentheses indicates that a method does not require any additional information and does not need any parameters. If there are parameters, they’re placed in a parameter-list—specified by the set of parentheses following the method’s name.

Subroutines: Methods That Do Not Return a Value A method can specify multiple parameters by separating each from the next with a comma—this is known as a comma-separated list. Each parameter has a variable name and a type. We refer to the part of the method that contains the keyword Sub, the method name and the parameter list as the method header.

Scope of Declarations Lifetime of a Variable Although a variable may not be in scope, it may still exist. A variable’s lifetime is the period during which the variable exists in memory. Some variables exist briefly, some are created and destroyed repeatedly, and others are maintained through the program’s execution. Variables normally exist as long as the construct in which they are declared exists—for instance, a local variable of a method will exist as the call to that method is still executing.

Scope of Declarations The basic scopes are: Block scope—The scope of a variable declared in a block is from the point of the declaration to the end of the block Method scope—The scope of a method’s local-variable declaration or parameter is from the point at which the declaration appears to the end of that method. Class scope—The scope of a member that's declared in the class’s body, but outside the bodies of the class’s methods is the entire body of the class.

Passing Arguments: Pass-by-Value vs. Pass-by-Reference Arguments are passed in one of two ways—pass-by-value or pass-by- reference. When an argument is passed by value, the program makes a copy of the argument’s value and passes the copy to the called method. With pass-by-value, changes to the called method’s copy do not affect the original variable’s value in the caller.

Functions: Methods That Return a Value A function is a method that returns a value to the caller. Function Declarations The format of a function declaration is Function method-name(parameter-list) As return-type declarations and statements End Function The method-name, parameter-list, and declarations and statements in a function declaration behave like the corresponding elements in a subroutine declaration. In the function header, the return-type indicates the type of the value returned from the function to its caller.

Optional Parameters Methods can have optional parameters. Declaring a parameter as Optional allows the calling method to vary the number of arguments to pass. An Optional parameter specifies a default value that's assigned to the parameter if the optional argument is omitted. You can create methods with one or more Optional parameters. All Optional parameters must be placed to the right of the method’s non-Optional parameters—that is, at the end of the parameter list.

Optional Parameters When a parameter is declared as Optional, the caller has the option of passing that particular argument. For example, the method header Function Power(ByVal base As Integer, Optional ByVal exponent As Integer = 2) As Integer specifies the last parameter exponent as Optional. Any call to Power must pass at least an argument for the parameter base, or a compilation error occurs. Optionally, a second argument (for the exponent parameter) can be passed to Power.

Optional Parameters Consider the following calls to Power: Power() Power(10) Power(10, 3) The first call generates a syntax error because a minimum of one argument is required for this method. The second call is valid because one argument (10) is being passed—the Optional exponent is not specified in the method call.

Method Overloading Method overloading allows you to create methods with the same name but different signatures—that is, there are different numbers and/or types of parameters, or they’re listed in a different order (by type). A method’s signature does not include its return type. When you call an overloaded method, the compiler selects the proper method by examining the number, types and order (by type) of the arguments.