1 / 8

Subprograms

Subprograms. A subprogram allows process (as opposed to data) abstraction. Characteristics single entry point caller suspended until control returns from subprogram control returns to caller when subprogram terminates only if subprogram terminates normally

edolie
Download Presentation

Subprograms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Subprograms • A subprogram allows process (as opposed to data) abstraction. • Characteristics • single entry point • caller suspended until control returns from subprogram • control returns to caller when subprogram terminates • only if subprogram terminates normally • exceptions allow for alternate control flow • Types • Functions: return a value • Procedures: do not return a value • Predicates: are provable (or not)

  2. Parameters • Formal parameters/parameters • in subprogram definition • Actual parameters/arguments • in subprogram call • Binding of arguments to parameters • positional (1st is bound to 1st, 2nd to 2nd, etc) • keyword (each parameter has a keyword

  3. Keyword parameter example (defun makePoint (&key x y) ...) (makePoint :x 3 :y 5) (makePoint :y 5 :x 3) This example is Lisp. Other languages that permit keyword arguments include Ada and Fortran95.

  4. Default arguments • C++, Fortran95, Ada and PHP allow default argument values void example(int x=100, int y) {...} example(30)  example(100,30) example(50,30)  example(50,30)

  5. Variable-length parameter lists • Some languages allow subprograms to accept a variable number of arguments. • Lisp, Scheme, C#, Java (define f (lambda (x y) ...)) f takes a list of two args (define g (lambda lst ...)) g takes a list of unspec len

  6. Java varargs example void foo(int x, String... names) { System.out.println(“x is ”+x); for (String name:names) { System.out.println(name); } } Vararg must be last parameter.

  7. Parameter passing modes • in • information passes in through parameter • out • information passes out through parameter • in-out • information passes both in and out through parameter

  8. passing modes • in-mode • pass-by-value • out-mode • pass-by-result • in-out mode • pass-by-value-result • pass-by-reference • (pass-by-name: textual substitution)

More Related