Programming Language Concept

One Stop Computer Science

Archive for December, 2017

Session VII: Subprograms

Setiap subprogram memiliki single entry point. Sebuah program yang dipanggil saat eksekusi disebut subprogram.

Local Variable

Keuntungan:

  • mendukung rekursi
  • penyimpanan untuk local variable dapat berbagi dengan beberapa subprogram

Kerugian:

  • Allocation/de-allocation, initialization time
  • Indirect addressing
  • Subprogram tidak history sensitive
  • Local variable dapat menjadi static.

Parameter Passing Methods

  1. Pass-by-Value

Nilai dari actual parameter digunakan untuk inisialisasi corresponding formal parameter. Dapat diimplementasikan dengan copying atau dapat pula diimplementasikan dengan mentransmit sebuah access path tetapi tidak direkomendasikan.

Kerugian bila menggunakan physical move: membutuhkan storage tambahan dan actual move dapat menimbulkan kerugian materi.

Kerugian bila menggunakan access path method: harus menggunakan write-protect dalam subprogram dan access memakan cost lebih.

2. Pass-by-Result

Dalam proses ini, tidak ada nilai yang ditransmit ke subprogram, corresponding formal parameter bertindak sebagai local variable. Membutuhkan extra storage dan copy operation.

3. Pass-by-Reference

Melalui sebuah access path. Biasa dikenal juga dengan nama pass-by-sharing.

Keuntungan: proses dilakukan dengan efisien (tanpa copying dan tanpa extra storage).

Kerugian: proses berjalan lebih lamban dibandingkan pass-by-value, berpotensi menimbulkan efek samping dan aliases yang tidak diinginkan.

Overloaded Subprogram

  • An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment.

  • Every version of an overloaded subprogram has a unique protocol.
  • C++, Java, C#, and Ada include predefined overloaded subprograms.

  • In Ada, the return type of an overloaded function can be used to disambiguate calls (thus two overloaded functions can have the same parameters).

  • Ada, Java, C++, and C# allow users to write multiple versions of subprograms with the same name.

Generic Subprogram

  • A generic or polymorphic subprogram takes parameters of different types on different activations.

  • Overloaded subprograms provide ad hoc polymorphism.

  • Subtype polymorphism means that a variable of type T can access any object of type T or any type derived from T (OOP languages).

  • A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism.

  • A cheap compile-time substitute for dynamic binding

  • 0 Comments
  • Filed under: Uncategorized
  • Control structure adalah sebuah stament yang eksekusinya dikontrol oleh control statement.

    Selection statement mengartikan pilihan antara dua atau lebih statement yang akan dieksekusi. Terdapat dua kategori, yaitu: two way selectors dan multiple way selectors.

    Contoh dari two way selectors adalah nesting selector. Contohnya dalam Java:

    if (sum == 0)

    if (count == 0)

    result = 0;

    else result = 1;

    Contohnya dalam C, C++, dan C#:

    if (sum == 0) {

    if (count == 0)

    result = 0;

    }

    else result = 1;

    Contohnya dalam bahasa Ruby:

    if sum == 0 then

    if count == 0 then

    result = 0

    else

    result = 1

    end

    end

    Contohnya dalam bahasa Phyton:

    if sum == 0 :

    if count == 0 :

    result = 0

    else :

    result = 1

    Contoh dari multiple way selectors adalah switch() case dan if else if.

    Contohnya dalam C, C++, Java, dan JavaScript:

      switch (expression) {

    case const_expr1: stmt1;

    case const_exprn: stmtn;

    [default: stmtn+1]

    }

    Contohnya dalam bahasa Ruby:

    leap = case

    when year % 400 == 0 then true

    when year % 100 == 0 then false

    else year % 4 == 0

    end

    Contohnya dalam Phyton:

    if count < 10 :

    bag1 = True

    elif count < 100 :

    bag2 = True

    elif count < 1000 :

    bag3 = True

     

    Iterative Statements

    iterative statements adalah sebuah perulangan eksekusi dari suatu statement atau compound statement yang dilakukan oleh iterasi maupun rekursi.

    Counter Controlled Loops

    Counter Controlled Loops adalah sebuah perhitungan iterative statement yang memiliki loop variable, memiliki initial dan terminal yang jelas, dan stepsize value.

    Logically Controlled Loops

    Logically controlled loops adalah perulangan yang dikontrol berdasarkan dari Boolean expression. Logically controlled loops dibagi menjadi 2, yaitu: pre-test dan post-test. Contoh dari pre-test adalah operasi while, dan contoh dari post-test adalah do-while. Contoh dalam bahasa C dan C++:

    • while (control_expr)

    loop body

    • do

    loop body

    while (control_expr)

    Iteration Based on Data Structures

    Contoh:

    • PHP

    – current points at one element of the array

    – next moves current to the next element

    – reset moves current to the first element

    • Java 5.0 (uses for, although it is called foreach)

    For arrays and any other class that implements the Iterable interface, e.g., ArrayList

    for (String myElement : myList) { … }

  • 0 Comments
  • Filed under: Uncategorized