Subprograms General concepts Parameter passing Functions

Slides:



Advertisements
Similar presentations
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
Advertisements

Programming Languages and Paradigms
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Subprogram ธนวัฒน์ แซ่ เอียบ. Subprograms as computational abstractions.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Lecture 16 Subroutine Calls and Parameter Passing Semantics Dragon: Sec. 7.5 Fischer: Sec Procedure declaration procedure p( a, b : integer, f :
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
1 Languages and Compilers (SProg og Oversættere) Sequence control and Subprogram Control.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
ISBN Chapter 10 Implementing Subprograms.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
1 CSCI 360 Survey Of Programming Languages 9 – Implementing Subprograms Spring, 2008 Doug L Hoffman, PhD.
Overview Parameter passing modes.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
CSI 3125, Subprograms, page 1 Subprograms General concepts Parameter passing Functions Subprograms as parameters.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues.
Chapter 9: Subprograms Introduction Fundamentals of Subprograms
ISBN Chapter 9 Subprograms. Corrected and improved by Assoc. Prof. Zeki Bayram, EMU, North Cyprus. Original Copyright © 2007 Addison-Wesley.
VHDL Discussion Subprograms IAY 0600 Digital Systems Design Alexander Sudnitson Tallinn University of Technology 1.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Procedure Definitions and Semantics Procedures support control abstraction in programming languages. In most programming languages, a procedure is defined.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
ISBN Chapter 10 Implementing Subprograms.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Implementing Subprograms
Chapter 9 Subprograms.
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
Functions.
Implementing Subprograms
Type Checking, and Scopes
CS 326 Programming Languages, Concepts and Implementation
Structure of Programming Languages
Principles of programming languages 4: Parameter passing, Scope rules
UNIT - V STORED PROCEDURE.
Organization of Programming Languages
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter 4: Writing Classes
User-Defined Functions
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Implementing Subprograms
Implementing Subprograms
VHDL Discussion Subprograms
Implementing Subprograms
Subprograms In Text: Chapter 9.
Chapter 9 Subprograms.
VHDL Discussion Subprograms
Subject : T0152 – Programming Language Concept
Languages and Compilers (SProg og Oversættere)
Chapter 9 Subprograms Fundamentals of Subprograms
Predefined Functions Revisited
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Implementing Subprograms
Implementing Subprograms
Chapter 10 Def: The subprogram call and return operations of
Presentation transcript:

Subprograms General concepts Parameter passing Functions Subprograms as parameters

General concepts Originally, subprograms (procedures and functions) were a mechanism for code reuse, a way to avoid repetition. Now they are seen as a fundamental abstraction mechanism. A subprogram is a named block (with a local scope) that encapsulates an algorithm. Semantically, a subprogram is a complex operation that can be initiated (called) as if it were an elementary operation. We talk about process abstraction.

A subprogram… is defined by means of lower-level operations, has a name, has a method of accepting arguments and delivering results (parameter passing and communication through non-local objects), is computed by suspending the calling program unit, executing the statement in this unit's block, and returning to the caller.

A subprogram body: a sequence of statements. A subprogram header (heading): name, parameter-passing modes, the type of the value returned (if the subprogram is a function). A subprogram body: a sequence of statements. A procedure extends the language: it is a new kind of statement. Similarly, a function is a new kind of operation.

Parameters A parameter of a subprogram is a generalization of an object manipulated by the statements—a subprogram is supposed to work in the same way for every parameter value. We also talk about “dummy variables”. By parameter passing we mean replacing generalized entities in a subprogram declaration (formal parameters) with existing objects (actual parameters).

Parameter passing A parameter passing mode determines how much of the argument is given to the subprogram: • only value • only address • address and value Also, what are the restrictions on the use of the argument: • read-only • write-only • read/write.

A simplified model of a procedure A procedure is a “black box” that reads in data and writes out results. in out in out

Pass by value Pass-by-value is much used in programming languages: in parameters in Ada; value parameters in Pascal; all parameters in Algol-68, C. Only the value of the parameter is given to the subprogram. To store this value, we use a local variable — its address. This is usually implemented by computing and copying the actual parameter’s value into the subprogram’s memory space. It may be expensive if the parameter is a large object, for example, an array.

Pass by result Pass-by-results is slightly bizarre but consistent with the general model. It is used in Ada: out parameters. The address (but not the value) of the parameter is made available to the subprogram—for write-only access. Technically, this is a local object that cannot appear in expressions.

Pass by value-result Where used: in out parameters in Ada. Also called pass-by-copy. When the subprogram is activated, the value of the parameter is copied into a local object. The final value of this object is copied back into the parameter.

Pass by value-result example -- procedure P(in out X: someType); -- some Q calls P(Z), Z is from Q begin X := Z; -- the body of P uses X Z := X; end; This method and pass-by-result may — in a particularly bizarre situation — be sensitive to the order in which parameters are evaluated (see the textbook).

Pass by reference Where used: var parameters in Pascal; all parameters in older Fortran. The address and value are both available to the subprogram’s statements. This is implemented by indirect addressing: the actual parameter is the address of a variable in the calling program unit. No copying is necessary.

Pass by reference example -- procedure P(var X: someType); -- some Q calls P(Z), Z is from Q begin -- the body of P uses Z end; Any changes made to Z inside P will be preserved when P terminates, and will be visible in Q.

A parameter-passing example, Pascal style program P; var J: integer; A: array [1..10] of integer; procedure SWAP( MODE X,Y: integer); var TMP: integer; begin TMP := X; X := Y; Y := TMP; end; begin J := 3; A[J] := 6; SWAP(J, A[J]); write(J, A[3]); end. MODE is in: J = 3, A[3] = 6 (no change, as expected) MODE is out: impossible (we cannot read the value of X) MODE is either ref or in out: J = 6, A[3] = 3 (a correct swap)

Points to consider… A big problem with pass-by-reference may be aliasing. One possible situation: passing the same variable as two different actual parameters. Another possibility: a non-local variable referenced in the subprogram and passed as a parameter. A parameter passed by result, value-result and reference must be a variable. How does pass-by-reference mode differ from pass-by-value-result?

Prolog is different The classification of formal parameters based on addresses and values is meaningful in imperative languages with updateable variables. It does not apply to Prolog, where addresses never available (in particular, there is no assignment!). append([a], [b], Result) in in out append(First, Second, [a,b]) out out in append([X,b], [c,Y], [a,P,Q,d]) none of the above!

Functions A function produces one value, returned by a special statement such as return someValue; or assigned to the function name (as in Pascal). When functions are used in expressions, they enrich the language. We can treat them as new operators. For example, if EXP(X, Y) has been defined to calculate XY, we can write Z := 2.0 * EXP(A, N); inside expressions, even if exponentiation is not supported by our programming language.

Functions (2) Side-effects in functions are a problem. Ideally, only in parameters (and no side effects) should be allowed in a mathematically correct function. If side-effects are really needed, a procedure should be used—with one parameter set aside to transmit the value that a function would compute. A design issue (orthogonality!): restricting types of objects that can be returned by functions.

Subprograms as parameters The concept is simple: an algorithm that depends on an embedded algorithm. Scheme has a perfect design; imperative languages are not as good. Consider the example of numerical integration—finding the area under a curve described by a function.

Subprograms as parameters, a Pascal example function DEFINITE_INTEGRAL( function F(ARG: real): real; LOW, HIGH: real): real; const N_INTERVALS = 1000; var DELTA, SUM, X: real; I: integer; begin DELTA := (HIGH - LOW) / N_INTERVALS; X := LOW; SUM := F(X); for I := 1 to N_INTERVALS - 1 do X := X + DELTA; SUM := SUM + 2.0 * F(X) end; SUM := SUM + F(X); DEFINITE_INTEGRAL := 0.5 * DELTA * SUM

Subprograms as parameters, a Pascal example (2) Any function that maps real  real can be passed to this “metafunction”: function SQ( A: real ): real; begin SQ := A * A end; {...} x := DEFINITE_INTEGRAL(SQ, 0.0, 9.0); This call produces (approximately!) 243.0.