Pemrograman VisualMinggu …6… Page 1 MINGGU Ke Enam Pemrograman Visual Pokok Bahasan: Module,...
-
Upload
gracie-lockett -
Category
Documents
-
view
220 -
download
3
Transcript of Pemrograman VisualMinggu …6… Page 1 MINGGU Ke Enam Pemrograman Visual Pokok Bahasan: Module,...
Minggu …6… Page 1
Pemrograman Visual
MINGGU Ke Enam
Pemrograman VisualPemrograman Visual
Pokok Bahasan:Module, Class & Methods
Tujuan Instruksional Khusus:Mahasiswa dapat menjelaskan dan mengaplikasikan konsep Methods pada Visual Basic 2008
Referensi:Deitel Deitel, Visual Basic 2008 (2009), How to Program, Prentice Hall. Chapter 7
Pemrograman Visual Minggu …6… Page 2
Agenda• Subroutines: Methods that do not return a value• Functions : Methods that Return a Value• Shared Methods and Class Math• Declaring Methods with Multiple parameter• Option Strict and Data-Type Conversions• Value Types and Reference Types• Pass-by-Value VS Pass-by-Reference• Scope of Declaration• Random number Generation• Method Overloading• Optional Parameter• Recursion
Pemrograman Visual Minggu …6… Page 3
Procedure or Subroutines
• Tidak mengembalikan nilai
• Bisa memiliki satu atau lebih parameter atau tidak sama sekali
• Examples: Panjang, Lebar dan Keliling Persegi Panjang
Pemrograman Visual Minggu …6… Page 4
Module Module1
Sub Main() hitungKeliling(2, 4) hitungKeliling(6, 5) hitungKeliling(10, 4) hitungKeliling(3, 10) Console.Readkey() End Sub
Sub hitungKeliling(ByVal panjang As Integer, ByVal lebar As
Integer) Console.WriteLine("Keliling Persegi
Panjang adalah {0}", panjang * lebar) End SubEnd Module
Example Procedure or Subroutines
Pemrograman Visual Minggu …6… Page 5
Function
• Mengembalikan nilai
• Bisa memiliki satu atau lebih parameter atau tidak sama sekali
• Examples: Panjang Lebar dan Keliling Persegi Panjang
Pemrograman Visual Minggu …6… Page 6
Module Module1
Sub Main() Console.WriteLine("Keliling Persegi
Panjang dari 2 dan 4 adalah {0}", hitungKeliling(2, 4))
Console.Readkey() End Sub Function hitungKeliling(ByVal panjang As
Integer, ByVal lebar As Integer) As Integer
Return 2 * (panjang + lebar) End FunctionEnd Module
Example Function
Pemrograman Visual Minggu …6… Page 7
Share Methods and Class Math
• Share methods are associated with the class, but not with any specific object of the class; they perform tasks that do not depend on the contents of any object
• You call a shared method by specifying the name of the class or module in which the method is declare, followed by the dot (.) separator and the method name.
• Any variable declared with keyword const is constant, its value cannot be changed after the variable is initiated. Constants are implicitly shared
Pemrograman Visual Minggu …6… Page 8
Example the Math Class Methods
Pemrograman Visual Minggu …6… Page 9
Declaring Methods with Multiple Parameter
• In a method with more than one parameter, the parameters are specified as a comma-separated list.
• There must be one argument in the method call for each parameter in the method declaration (except in the case of optional parameters). The type of each argument must be consistent with the type of the corresponding parameter
• Methods can return at most one value, but the returned value could be a reference to an object that contains many values.
Give Example about Declaring Methods with Multiple parameter Figure 07_05 and 07_06
Pemrograman Visual Minggu …6… Page 10
Option Strict and Data-Type Conversions
• Option Explicit forces you to explicitly declare all variables before they are used in a program.
• Option Strict cause the compiler to check all conversions and requires you to perform an explicit conversion for all narrowing conversions that could cause data loss or program termination.
Pemrograman Visual Minggu …6… Page 11
Value Types and Reference Types• Both value types and reference types include primitive
types and types that you can create. The primitive value type include the integral types (Byte, SByte, Short, UShort, Integer, UInteger, Long and ULong), the floating-point types (Single and Double) and types Boolean, Date, Decimal, and Char. There is only one primitive reference type String.
• Reference types can be created by defining classes, modules, interface and delegates. Value type can be created by defining a structures and enumerations.
Give Example about Value Types and Reference Types Figure 07_11
Pemrograman Visual Minggu …6… Page 12
Pass-by-Value VS Pass-by-reference
• When an argument is passes by value, the program makes a copy of the argument’s value and passes that copy to the called method. With pass-by-value, changes to the called method’s copy do not effect the original variable’s value.
• When an argument is passed by reference, the caller gives the called method the ability to access and modify the caller’s original data directly.
Pemrograman Visual Minggu …6… Page 13
Scope of Declaration
• The scope of a declaration is the portion of the program that can refer to the declared entity by it’s name, without qualification.
• If a local variable or parameter in a method has the same name as a field, the member is “hidden” until the block terminates execution--this is called shadowing. Give Example about Scope of Declaration Figure 07_12 – 07_13
Pemrograman Visual Minggu …6… Page 14
Random Number Generation
Pemrograman Visual Minggu …6… Page 15
Method Overloading
• Method overloading allows you to create multiple methods with the same name but differing numbers and types of parameters or with different orders of the parameters (by type)
• Overloaded methods are distinguished by their signatures, which are a combination of the method’s name and parameter types, and the order of the parameter (by type)
Pemrograman Visual Minggu …6… Page 16
Example Method Overloading
Pemrograman Visual Minggu …6… Page 17
Optional Parameter• Methods can have optional parameters. Declaring a
parameter as optional allows the calling method to vary the number of arguments to pass.
• Optional parameters specify a default value that is assigned to the parameter if the optional argument is not passed.
• For an optional parameter, the caller has the option of passing that particular argument.
• You can create methods with one or more optional parameters. All optional parameter, however must be placed to the right of the method’s non optional parameters.
• Default values can be used only with parameters declared as Optional
• String method IsNullOrEmpty returns True if its argument is the value Nothing or the empty string;otherwise, it returns False
Pemrograman Visual Minggu …6… Page 18
Example Optional Parameter
Pemrograman Visual Minggu …6… Page 19
Recursion
• A recursive methods calls itself either directly or indirectly (i.e., through another method)
• A recursive method knows how to solve only the simplest case(s)—the base case(s). If the method is called with a base case, it returns a result. If the method is called with a more complex problem, it divides the problem into a base case and a recursion step that resembles the original problem but is a slightly simpler or smaller version of it.
• The recursion step executes while the original call to the method is still “open”. The recursion step can result in many more recursive calls as the method divides each new sub problem into two conceptual pieces
Pemrograman Visual Minggu …6… Page 20
Example Recursion
Pemrograman Visual Minggu …6… Page 21
Questions
&
Answers
Pemrograman Visual Minggu …6… Page 22
Thank You