Monday 20 April 2015

9.0 Sub Programs

9.2 Fundamentals of subprograms
- allow process abstractions
- each has a single entry point.
- the calling program unit is suspended during the execution of the call subprogram.
- parameters are a way of giving a subprograms access to the data they are to process

there are two types of subprograms - function and sub procedures

A Sub procedure is a separate procedure that can take arguments, perform a series of statements, and change the value of its arguments, it doesn't require to return a value, but it cannot be used in an expression. (expressed with void in C#)

A Function
procedure, is a procedure that can take arguments, perform a series of statements, and change the value of its arguments, but it also must return a value and it can be used in an expression.

- local variables = Variables that are defined inside subprograms.

subprograms type check parameters

9.5 Parameter passing methods

9.5.1 Pass by value 
- creates a deep copy of the object passed in from the caller, not a reference to the original object.

- if the deep copy gets changed with the subprogram, the callers object will not be changed.


9.5.2 Pass-by-Result
Pass-by-result is an implementation model for out mode parameters.
- helps assigning new values to multiple value-types.

ie. void Fixer(out int x, out int y)

 9.5.3 Pass-by-Value-Result
 - a combination of pass-by-value and pass-by-result

ie. void Fixer(int x, out int y)


 9.5.4 Pass-by-Reference
 - passes in an memory address of the object (shallow copy) , to the subprogram
- if the object is modified within the subprogram, the callers object will persist those changes.


9.7 Overloaded Subprograms
An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment. Every version of an  overloaded subprogram must have a unique protocol, that is, it must be different from the others in the number, order, or types of parameters or in return type. 


9.8 Generic Subprograms (Generic Method)

-Software reuse can be an important contribute to software productivity.

-One way to re-use, is to lessen the need to create different subprograms to do the same thing for different data-types


- A polymorphic subprogram takes parameters of different type on different activations.

- Parametric polymorphism - is provided by a subprogram hat takes generic parameters that are used in type expressions that describe the types of the parameters of the subprogram.

i.e of a generic method
. public static T doit<t> (t pl)


No comments:

Post a Comment