1 CHAPTER 9 SUBPROGRAM. 2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures.

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

Programming Languages and Paradigms
Names and Bindings.
Overview Fundamentals of Subprograms (Sec )
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Chapter 9 Subprograms.
Chapter 9 Subprograms.
Chapter 5 Names, Bindings, and Scopes
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Chapter 9 Subprograms Sections 1-5 and 9. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 10 Implementing Subprograms.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 5 Names, Bindings, Type Checking, and Scopes Names Variables The Concept of Binding Type Checking Strong Typing Type Compatibility.
ISBN Chapter 9 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Introduction Two fundamental abstraction facilities.
ISBN Chapter 9 Subprograms. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.9-2 Chapter 9 Topics Introduction Fundamentals.
ISBN Chapter 9 Subprograms CE2004 Principles of Programming Languages.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
Scope.
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
1 Subprograms Fundamentals of subprograms Design issues for subprograms –Parameter-passing methods –Type checking of parameters –Static or dynamic storage.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
Subprograms Support process abstraction and modularity –characteristics of subprograms are that they have a single entry point the calling entity is suspended.
Chapter 9 Subprograms Fundamentals of Subprograms
1 Chapter 9 © 2002 by Addison Wesley Longman, Inc. 9.2 Fundamentals of Subprograms - General characteristics of subprograms: 1. A subprogram has a single.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 8 Fundamental Characteristics of Subprograms 1. A subprogram has a single entry point 2. The.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Week 7 Subprograms. Copyright © 2006 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for.
Subprograms subroutines, procedures, functions, methods.
ISBN Chapter 9 Subprograms. Copyright © 2015 Pearson. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Programming Languages and Paradigms Imperative Programming.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
Chapter 9 Subprograms. 2 Fundamentals of Subprograms Each subprogram has a single entry point The calling program is suspended during execution of the.
ISBN Chapter 9 Subprograms Sections
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
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.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
Names, Scope, and Bindings Programming Languages and Paradigms.
Names, Bindings, Type Checking and Scopes. Chapter 5 Topics Introduction Names Variables The Concept of Binding Type Checking Strong Typing Type Equivalence.
Chapter 9 Chapter 9 Subprograms. The Structure of Run-Time Memory.
ISBN Chapter 9 Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved.1-2 Chapter 9 Topics Introduction Fundamentals of Subprograms.
Chapter 9 Subprograms. Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing.
Chapter 9 Subprograms.
Chapter 9 Subprograms.
Implementing Subprograms
Type Checking, and Scopes
Names, Bindings, and Scopes
CSCI 3370: Principles of Programming Languages Chapter 9 Subprograms
Chapter 10: Implementing Subprograms Sangho Ha
Chapter 9 Subprograms.
CSC 533: Programming Languages Spring 2015
Chapter 9 Subprograms.
Chapter 9: Subprograms Sangho Ha
Subprograms In Text: Chapter 9.
Chapter 9 Subprograms.
Subject : T0152 – Programming Language Concept
Chapter 9 Subprograms Fundamentals of Subprograms
Chapter 9 Subprograms.
Chapter 9 Subprograms.
CSC 533: Programming Languages Spring 2018
Chapter 9 Subprograms Subprograms are the fundamental building blocks of programs and are therefore the most important concepts in programming language.
CSC 533: Programming Languages Spring 2019
Presentation transcript:

1 CHAPTER 9 SUBPROGRAM

2 SUBPROGRAM Topics: b Definitions of subprogram b general subprogram characteristics b parameters b Functions and procedures b Design issues for subprogram b Parameter passing method b Model for parameter passing b Overload subprogram b Type checking & referencing environment b Subprogram passed method b Generic subprogram

3 Definitions : Subprogram : A program separate from the main program that executes aseries Of operations that occures multiple times during the machine cycle. Subprogram : describes the interface to and the actions of the subprogram abstraction. Subprogram call : is the explicit request that the called subprograms be executed. executed. Process abstraction are presented in programming languages by subprograms

4 What does it mean for a subproram to be active ? Subprogram : is to be active if after having been called,it has begun execution but has not yet completed that execution Execution time Execution time Duration time Subprogram(x) start Subprogram(x) start

5 Definitions : A Subprogram header : is the first line of the definition,serves several purposes. First is specifies that the following syntactic unit is a subprogram's kind is often accomplished with a special word. is the first line of the definition,serves several purposes. First is specifies that the following syntactic unit is a subprogram's kind is often accomplished with a special word. The header contains some keyword signalin the beginning of asubproram, a name for the subprogram,and Header it may optionally specify alist of parameters e.g: (Fortran) Subroutine Adder (parameters)

6 e.g: (C) Void adder (parameters),would serve as the header of a SUBPROGRAM named adder,where void indicates that it does not return a value b Prototype :is a subprogram declaration providing Interface information Definitions :

7 Parameter profile: Describes the number,order, and types of the parameters **also called “signature”.

8 General subprogram characteristics : b Each subprogram has a single entry point b The calling program unit is suspend during The execution of the called subprogram, which means that there is only one subprogram in execution at any time b Control always return to the caller when the subprogram execution terminates

9 Parameters : There are two ways that subprogram can gain access to the data that it is to process: b Direct access to nonlocal variables b Parameter passing ** Parameter passing is more flexible than direct access to nonlocal variables

10 b Formal parameters : The parameters in the subprogram header b Actual parameters : alist of parameters that appear in subprogram call statement b Positional parameters: is a method for binding actual parameter to formal parameter, is done by position ** ((the first actual parameters is bound to the first formal parameter and so forth such parameters are called positional parameters )) ** ((the first actual parameters is bound to the first formal parameter and so forth such parameters are called positional parameters )) e.g : M = rect (a, b,c ) // subproram call Float rect ( int x, int y, int z ) Parameters : (count …)

11 Keyword parameters : The name of the formal parameter to which an actual parameter is to be bound is specified with the actual parameter Advantage: That they can appear in any order in the actual parameter list Disadvantage: That the user of the subprogram must know the names of formal parameters Parameters : (count …)

12 Subprogram example: Int cube(int); prototype Int main(){ Int y=5; actual parameters Int y=5; actual parameters Cout<<cube(y); Subprogram call Cout<<cube(y); Subprogram call Int x=3; Int x=3; Int cube (int x); subprogram header Int cube (int x); subprogram header{ formal parameter formal parameter return x*x; }}

13 The Two kinds of subprograms : There are two distinct categories of subprograms: b Procedures b Functions

14 Procedures : are collection of statements that define parameterized computations, are collection of statements that define parameterized computations, these computations are enacted by single call statements. these computations are enacted by single call statements. Procedure are called subroutines Procedures has Two parts : The speification and the Body The specification is begins with the keyword PROCEDURE and ends With the procedure name or parameter list e.G : PROCEDURE a_test(a,b : in integer ; c:out Integer)

15 Procedures : Procedure identifier Rules : b The name must start with aletter or made out of letters, numbers and the underscore( _ ). b the name is always case-sensetive ( uppercase / lowercase names are identical). b the name may not start with the dollar-sign ($).

16 Examples of procedures Program example1; Specification Specification procedure add; var x : integer ; x : integer ; y : integer ; y : integer ; begin BODY read ( x, y ); read ( x, y ); write ( x + y ); write ( x + y );end;BEGINadd; END. Program example2 ; Var x : integer ; y : integer ; Procedure add ( a : integer ; b : integer ); begin write ( a + b ); write ( a + b );end;BEGIN x = 10 ; y = 5 ; add ( 10, 5 ) ; END.

17 Functions : Functions : structurally resemble procedure, But are semantically modeled on mathematical functions ( Functions are procedures which return value). ** Function are called by appearances of their name in expressions e.g: (function header and call ) Void sort (int list[], int listlen); // function header … Sort(scores,100); // function call

18 Functions : ** In function body The return statement end the execution of the function and return the value of the expression enclosed In the braces as a result **The function body can contain any number of return statement

19 Functions : // function example : Function minimum ( A,B : Integer) return integer is Begin If A<= B then Return A ; Else Return B ; End if ; End minimum;

20 Design Issues For Functions : There are Two design issues are specific to functions : b Functional side effect The evaluation of a function effect the the value of other operands in the same expression. ** (solution parameters to functios can have only in mode formal parameter) b Types of returned value most imperative programming languages restrict the types that can be returned by their functions. C allow any type to be returned by its function except arrays and functions “both of these can be handled by pointer type return values ” C allow any type to be returned by its function except arrays and functions “both of these can be handled by pointer type return values ” Ada language its function can return values of any type Ada language its function can return values of any type

21 QUESTIONS?

22 Design issues for subprogram : b What parameter passing method use ? b Are the type of the actual parameter checked against the formal parameter ? b Are local variables statically or dynamically allocated? b If subprogram can be passed as parameters what is the referencing environment of such subprogram ? b Can subprogram be overloaded ? b Can subprogram be generic ? b Is either separate or independent compilation possible ?

23 Parameter passing methods : b Parameter passing methods are the ways in which parameters are transmitted to and/or from calling subprogram, b Formal parameters are characterized by one of three distinct semantics models : 1. They can receive data from the corresponding actual parameter (in mode). 2. They can transmit data to the actual parameter (out mode). 3. They can do both (inout mode).

24 Graph for parameter passing methods :

25 Implementation models for parameter passing : b Pass by value b Pass by result b Pass by value result b Pass by reference b Pass by name

26 Pass by value : When parameter is passed by value,the value of the actual parameter is used to initialize the correspondin formal parameter,which then act as alocal variable in the subprogram b Advantage: simple mechanism,actual parameter can be expression or values, (formal parameters are write protected) does not cause side effect. b Disadvantage: needs additional storage for formal parameters.

27 Pass by result : b Formal parameter acts as local variable just before control returns to caller, value is passed to actual parameter, which must be a variable b problem #1: Actual parameter collision, such as call sub(p1,p1) may return different values to p1 depending on order of assignment b Problem #2: Address evaluation may be done at call or at return, for example list[index] if the index is changed by the subprogram,either through global access or as formal parameter,ten the address of list[index] will changed between the call and the return

28 Pass by value result : b The value of the actual parameter is used to initialize the corresponding formal parameter,the value of the formal parameter is transmitted back to the actual parameter b Pass by value result is some times call pass by copy b Pass by value result share with pass by value and pass by result the disadvantage of requiring multiple storage for parameters and time for copying values. b Pass by value result share with pass by result the problems associated with the order in which actual parameters are assigned.

29 Pass by reference : b In pass by reference method transmits an access path,usually just an address, to the called subprogram b The called subprogram is allowed to access the actual parameter in the calling program unit.

30 Pass by reference : (count …) b The advantage of Pass by reference : Passing process is efficient, in term of both time and space, duplicate space is not required, nor is any copying required. b The disadvantage of Pass by reference : 1. access to the formal parameters will be slower,because of the additional level of indirect addressing that is required 2. if only one way communication to the called subprogram is required,inadvertent and erroneous changes may be made to the actual parameter 3. the aliases can be created

31 Pass by name : b The actual parameter is textually substituted for the formal parameter in all its occurrences in the subprogram. Advantage: b More flexible than other methods. Disadvantage : b Relatively slow than other methods b Very expensive

32 Parameter passing example: Program ParamPassing; var i : integer; a : array[1..2] of integer; procedure p(x, y : integer); begin x := x + 1; i := i + 1; y := y + 1; end; {p} begin {ParamPassing} a[1] := 1; a[2] := 1; i := 1; p(a[i], a[i]); writeln(‘a[1] = ’, a[1]); writeln(‘a[2] = ’, a[2]); end. Pass by value: a[1] = 1 a[2] = 1 Pass by result: x and y have no initial values Pass by value-result: a[1] = 2 a[2] = 1 Pass by reference: a[1] = 3 a[2] = 1 Pass by name: a[1] = 2 a[2] = 2

33 Overload Subprogram b Overload Subprogram is a subprogram that has the same name as another subprogram in the same referencing environment b Overloaded subprograms that have default parameters can lead to ambiguous subprogram calls.  Example: (C++) void fun( float b = 0.0 ); void fun(); … fun ( );

34 QUESTIONS?

35 Type checking parameter : b Without type checking,some typographical errors lead to program error difficult to diagnose because they are not detected by the compiler or the run time system. b Early programming language,such as FORTRAN 77 and the original version of c did not required parameter type checking,most later language require it, the relatively recent language Perl, JavaScript, and PHP do not

36 Example of type checking parameter : double sin(x) double x; { …. } Using this method avoids type checking, thereby allowing calls such as: Double value; int count; … Value = sin (count); To be legal, although they are never correct

37 Subprogram passed as parameters: Some language allow nested subprogram ((allow subprogram to be a parameter for another subprogram)) So in this case the following choices for referencing environment: So in this case the following choices for referencing environment: 1. Shallow binding : the environment of the call statement that enacts the passed subprogram. 2. Deep binding : the environment of the definition of the passed subprogram. of the passed subprogram. 3. Ad hoc binding : the environment of the call statement that passed the subprogram as an actual parameter

38 Generic Subprogram b A generic or polymorphic subprogram is one that takes parameters of different types on different activations. b Overloaded subprograms provide ad hoc polymorphism. b A subprogram that takes a generic parameter that is used in a type expression that describes the type of the parameters of the subprogram provides parametric polymorphism.

39 Advantages to using subprograms b There are several advantages to using subprograms :  They help keep the code simple, and, thus, more readable;  They allow the programmer to use the same code as many times as needed throughout the program;  They allow the programmer to define needed functions; and,  They can be used in other programs