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