Subprogram (Function and Procedure)

BAB VII
Subprogram
Subprogram is a smaller program in a program.
Subprogram can also be called procedure or function, depending on their return value.
Procedure is a subprogram that doesn’t have a return value.
Function is a subprogram that has a return value.
Each subprogram has their own local variables or sometimes they don’t, depending on the programmer, and what the programmer need from the subprogram.
There are two types of local-variable
·         Stack Dynamic = support recursion, has a different address whenever it’s called.
·         Static = doesn’t support recursion, has the same address whenever it’s called.
Models of Parameter Passing
·         Pass-by-Value
·         Pass-by-Result
·         Pass-by-Value-Result
·         Pass-by-Reference
Overloaded Subprograms
An overloaded subprogram is one that has the same name as another subprogram in the same referencing environment
Generic Subprograms
A generic or polymorphic subprogram takes parameters of different types on different activations
Closures
A closure is a subprogram and the referencing environment where it was defined
Subprogram is very useful, when we program something, it helps by decreasing the repetition of something we do, for example
·         We want to count the factorial of N. we can create a subprogram to handle that, and just call the subprogram.
·         Count the frequencies of alphabet appearing in a word or sentences.
·         Distinguishing whether the input is a valid input or not


Tidak ada komentar:

Posting Komentar