Professional Documents
Culture Documents
Summer 2010
Denition
Subprogram
A piece of program Well-dened operational semantics Can be executed (called) from various places in a program
Subprograms:
1. Eliminate need to replicate code 2. Process abstraction
Programmer can make use of its operation Doesnt need to be concerned with details of how that operation is implemented
Major types
1. Procedure: executed for its side-eects 2. Function: executed for its value 3. Relational: executed for instances (Prolog) 4. Method: attached to objects More in common than not Discussion will mostly be about them all
General characteristics
Sebesta (pp. 392393) Name: A subprogram has a name Entry point: A subprogram has a single entry point Execution: One one subprogram executing at a time
Others may be active but suspended
Calling subprograms
Function call
1. Function name 2. List of actual parameters Syntactically an expression Can be used wherever an expression of the functions return type is expected C-based languages allow a statement to be nothing but a function call
Its value is discarded
Calling subprograms
Prolog relational subprogram
1. Relation name 2. List of argument expressions
Could have variables in them
$ prolog | ?- [user]. | max(X,Y,Z) :X>Y, Z=X; Z=Y. | printmax(X,Y) :max(X,Y,Z), format("The max of d and d is d.n", [X,Y,Z]). | yes | ?- printmax(3,5). The max of 3 and 5 is 5. yes | ?- printmax(7,2). The max of 7 and 2 is 7. yes
Body: The code to be executed when called Header can sometimes appear without the body
If the compiler needs header information to compile calls But programmer does not want to specify details yet Example: C function denition
Subprogram bodies
True for most current languages, some older PLs worked dierently
Subprogram termination
Fall-through
A procedure may return when execution falls through to the bottom The body is syntactically one statement The procedure returns when that statement has been executed
Return
We may use a return statement An executable statement Terminate the procedure Return execution to calling unit return is a variety of exit that exits a procedure return <expression>
Function terminates Value of <expression> is the value of the function
Parallel assignment
We saw assignment statements with multiple variables on the LHS
No return value
Python function may return no value
>>> def nothing(): ... return ... >>> nothing() >>>
Header: Name
Most subprograms have names It is possible to create a nameless function Python example
>>> (lambda x: 4 x+1)(3)
Name, when exists, follows syntactic rules for an identier In statically scoped languages
Scope is enclosing block
Header: Type
Function subprograms have a type Type of the returned value Procedures dont return a value
So usually dont have a type
In C-based languages
A procedure is a function whose type is void
Positional parameters
Usual method for matching actual to formal parameters nth actual parameter is matched with nth formal parameter
Optional parameters
In some languages it is possible to have optional parameters # of actual parameters < # of formal parameters Extra formal parameters are treated like local variables
Initialized to default value, if any
Keyword parameters
Formal parameter is mentioned with actual parameter in call Indicates they should be matched Common Lisp example
cl-user(14): (defun testKey (&key first second third) (list first second third)) testKey cl-user(15): (testKey :second 2 :third c :first I) (I 2 c) cl-user(16): (member c (a b c d e)) (c d e) cl-user(17): (member (3 c) ((1 a) (2 b) (3 c) (4 d) (5 e))) nil cl-user(18): (member (3 c) ((1 a) (2 b) (3 c) (4 d) (5 e)) :test #equal) ((3 c) (4 d) (5 e)) cl-user(19): (member March ((January 31) (February 28) (March 31) (April 30)) :key #first) ((March 31) (April 30))
Default values
Optional or keyword formal parameters can have a default value Used if subprogram is called without a matching actual parameter Common Lisp example
cl-user(6): (defun packem (w x &optional (y 5) (z 7)) (list w x y z)) packem cl-user(7): (packem 1 3) (1 3 5 7) cl-user(8): (packem 1 3 11) (1 3 11 7)
Parameter passing
2. Out mode
Value of actual parameter not made available to subprogram Value can be changed/set by subprogram
3. Inout mode
Value of actual parameter made available via formal parameter Value can be changed by subprogram Prolog
Pass-by-name
Inout mode technique Actual parameter expression texturally replaces formal parameter REQUIRED: Read discussion of Common Lisp macros in Shapiros lecture notes
Work through examples on your own This material is REQUIRED This material may be on the FINAL
Pass-by-value
In mode technique Formal parameter is bound to its own memory cell at call time Value of actual parameter is copied into it Formal parameter acts like a local variable within subprogram Formal parameter deallocated at subprogram termination This is the model you are most used to from Java
Pass-by-result
Out mode technique Formal parameter acts like an uninitialized local variable At subprogram termination time its value is copied into the l -value of the actual parameter C# uses this technique
Example and discussion of C# ref and out keywords in Shapiro notes
Pass-by-value-result
Inout mode technique Acts like pass-by-value at call time Acts like pass-by-result at termination Formal parameter is bound to its own memory cell
Distinct from that of the actual parameter
Pass-by-reference
Inout mode technique Formal parameter bound to same memory cell as actual parameter Formal parameter is an alias of actual parameter C++ example
Subtleties
Java
All parameter passing in Java is pass-by-value However, many variables hold references to objects So although the parameter is passed by value, the object is essentially passed by reference
C
Its sometimes said that C uses pass-by-value except for arrays, which are passed by reference Untrue C always uses pass-by-value Array-valued variables are actually pointers that point to rst element in array Similar to situation with objects in Java
Function parameters
Function as a data type Functions can be passed as parameters Common Lisp has functions that apply functions to their arguments As does Python:
Listing 2: printTable.py
def doubleit(x): return 2*x def sqr(x): return x*x def printTable(f, args): for i in args: print i, apply(f, (i,)) printTable(doubleit, [1,2,3,4,5]) print printTable(sqr, [1,2,3,4,5]) $ 1 2 3 4 5 1 2 3 4 5 python printTable.py 2 4 6 8 10 1 4 9 16 25
Function parameters in C
In C an actual parameter may be the name of a function Matching formal parameter must be a pointer to a function returning the correct type The name of a function is a constant pointer to the function. Its value is the address of the functions machine code in memory. C Pocket Reference, Prinz & Kirch-Prinz, translated by Tony Crawford, OReilly 2002.
Overloaded subprograms
One name associated with multiple protocols Each with its own body Dispatching is the process of choosing which body to execute
1. Compare call with various protocols associated with name 2. Decide which protocol to execute
Polymorphic subprograms
Polymorphic subprograms dispatch on type of arguments Heart of object-oriented programming Each class in a class hierarchy can dene its own version of a method Which method body to execute is dynamically decided
Based on type of principal actual argument Or on type of several actual arguments
Commonly used in Java for constructors Can be used to give the eect of optional parameters
Which Java doesnt have
Dispatch by unication
Prolog uses unication More in Knowledge Representation course See also Shapiros notes
Further topics