Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com.

Slides:



Advertisements
Similar presentations
Modules Program is built out of components. Each component defines a set of logically related entities (strong internal coupling) A component has a public.
Advertisements

Slide: 1 Copyright © AdaCore Record Types Presented by Quentin Ochem university.adacore.com.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Slide: 1 Copyright © AdaCore Packages Presented by Quentin Ochem university.adacore.com.
Programming Languages and Paradigms
Programming Languages and Paradigms The C Programming Language.
Slide: 1 Copyright © AdaCore Arrays Presented Quentin Ochem university.adacore.com.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Gary MarsdenSlide 1University of Cape Town Statements & Expressions Gary Marsden Semester 2 – 2000.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
ISBN Chapter 10 Implementing Subprograms.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
Topics of Lecture Structural Model Procedures Functions Overloading.
Semantics of Calls and Returns
Approaches to Typing Programming Languages Robert Dewar.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
ISBN Chapter 9 Subprograms and Functions –Design Issues –Local Referencing Environments –Parameter-Passing Methods –Parameters that are Subprogram.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Imperative Programming
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
Names Variables Type Checking Strong Typing Type Compatibility 1.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
CS 2104 Prog. Lang. Concepts Subprograms
Slide: 1 Copyright © AdaCore Basic Types Presented by Quentin Ochem university.adacore.com.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Chapter 10 Implementing Subprograms. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 10 Topics The General Semantics of Calls and Returns.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
EECE 310: Software Engineering Iteration Abstraction.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Programming Languages and Paradigms Imperative Programming.
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
1 Subprograms In Text: Chapter 8. 2 Chapter 8: Subprograms Outline Definitions Referencing environments Parameter passing modes and mechanisms Independent.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms What actions must take place when subprograms are called and when they terminate? –calling a subprogram has several associated.
1 © AdaCore under the GNU Free Documentation License Franco Gasperoni
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Slide: 1 Copyright © AdaCore Your First Ada Program Presented by Quentin Ochem University.adacore.com.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Implementing Subprograms
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Names, Scope, and Bindings Programming Languages and Paradigms.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
ISBN Chapter 10 Implementing Subprograms.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Implementing Subprograms
Definition of the Programming Language CPRL
Chapter 10 : Implementing Subprograms
Implementing Subprograms Chapter 10
EECE 310: Software Engineering
Chapter 7: User-Defined Functions II
Implementing Subprograms
Subprograms In Text: Chapter 9.
PZ09A - Activation records
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Java Programming Language
Corresponds with Chapter 5
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Programming II Vocabulary Review Loops & functions
Implementing Subprograms
Presentation transcript:

Slide: 1 Copyright © AdaCore Subprograms Presented by Quentin Ochem university.adacore.com

Slide: 2 Copyright © AdaCore Subprograms in Ada: Specifications Ada differentiates functions (returning values) and procedures (with no return values) function F (V : Integer) return Integer; procedure P (V : in out Integer); int F (int V); void P (int *V); Subprogram name parameter name Parameter type Return type Parameter mode AdaC++

Slide: 3 Copyright © AdaCore Subprograms in Ada: Declaration and Body Declaration is optional, but must be given before use Functions result cannot be ignored Completion / body is introduced by “is” function F (V : Integer) return Integer is R : Integer := V * 2; begin R := R * 2; return R - 1; end F; function F (V : Integer) return Integer; Declaration Body

Slide: 4 Copyright © AdaCore Parameter Modes Mode "in" –Specifies that actual parameter is not altered –Only reading of formals is allowed –Default mode Mode "out" –Actual is expected to be altered –Writing is expected, but reading is also allowed –Initial value is not defined Mode "in out" –Actual is expected to be both read and altered –Both reading & updating of formals is allowed function F (V : in out Integer) return Integer is R : Integer := V * 2; begin V := 0; R := R * 2; return R - 1; end F;

Slide: 5 Copyright © AdaCore Parameter Passing Mechanisms Passed either “by-copy” or “by-reference” By-Copy –The formal denotes a separate object from the actual –A copy of the actual is placed into the formal before the call –A copy of the formal is placed back into the actual after the call By-Reference –The formal denotes a view of the actual –Reads and updates to the formal directly affect the actual Parameter types control mechanism selection –Not the parameter modes

Slide: 6 Copyright © AdaCore Standardized Parameter Passing Rules By-Copy types –Scalar types –Access types –Private types that are fully defined as by-copy types By-Reference types –Tagged types –Task types and Protected types –Limited types –Composite types with by-reference component types –Private types that are fully defined as by-reference types Implementation-defined types –Array types containing only by-copy components –Non-limited record types containing only by-copy components –Implementation chooses most efficient method

Slide: 7 Copyright © AdaCore Subprogram Calls If no parameter is given, no parenthesis is allowed Named argument is possible out and in out modes require an object function F return Integer; V : Integer := F; procedure P (A, B, C : Integer); P (B => 0, C => 0, A => 1); procedure P (X : out Integer); V : Integer; P (V);

Slide: 8 Copyright © AdaCore Default Values “in” parameters can be provided with a default value Default values are dynamic expressions, evaluated a the point of call if no explicit expression is given procedure P (A : Integer := 0; B : Integer := 0); P; -- A = 0, B = 0; P (1); -- A = 1, B = 0; P (B => 2); -- A = 0, B = 2; P (1, 2); -- A = 1, B = 2;

Slide: 9 Copyright © AdaCore Indefinite Parameters and Return Types Subprograms can have indefinite parameters and return types Constraints are computed at the point of call Don’t assume boundaries! function Comment (Stmt : String) return String is begin return "/*" & Stmt & "*/"; end Comment; S : String := Comment ("a=0"); -- return /*a=0*/ procedure Init (Stmt : in out String) is begin for J in 1.. Stmt'Length loop -- Incorrect Stmt (J) := ' '; end loop; end Init; S : String := "ABCxxx"; begin Init (S (4.. 6));

Slide: 10 Copyright © AdaCore Aliasing Ada has to detect “obvious” aliasing errors function Change (X, Y : in out Integer) return Integer is begin X := X * 2; Y := Y * 4; return X + Y; end; One : Integer := 2; Two : Integer := 4; begin Two := Change (One, One); -- warning: writable actual for "X" overlaps with actual for "Y" Two := Change (One, Two) - Change (One, Two); -- warning: result may differ if evaluated after other actual in expression

Slide: 11 Copyright © AdaCore Overloading (1/2) Ada allows overloading of subprograms Overloading is allowed if specification differ by –Number of parameters –Type of parameters –Result type Some aspects of the specification are not taken into account –Parameter names –Parameter subtypes –Parameter modes –Parameter default expressions procedure Print (V : Integer); procedure Print (V : Float); subtype Positive is Integer range 1.. Integer'Last; procedure Print (V : Integer); procedure Print (W : out Positive); -- NOK

Slide: 12 Copyright © AdaCore Overloading (2/2) Overloading may introduce ambiguities at call time Ambiguities can be solved with additional information type Apples is new Integer; type Oranges is new Integer; procedure Print (Nb_Apples : Apples); procedure Print (Nb_Oranges : Oranges); N_A : Apples := 0; begin Print (N_A); -- OK Print (0); -- NOK Print (Oranges'(0)); -- OK Print (Nb_Oranges => 0); -- OK

Slide: 13 Copyright © AdaCore Operator Overloading Default operators (=, /=, *, /, +, -, >, =, <=, and, or…) can be overloaded, added or removed for types “=“ overloading will automatically generate the corresponding “/=“ type Distance is new Float; type Surface is new Float; function "*" (L, R : Distance) return Distance is abstract; -- removes "*" function "*" (L, R : Surface) return Surface is abstract; -- removes "*" function "*" (L, R : Distance) return Surface; -- adds "*" type Rec is record Unimportant_Field : Integer; Important_Field : Integer; end record; function "=" (L, R : Rec) return Boolean is begin return L.Important_Field = R.Important_Field; end "=";

Slide: 14 Copyright © AdaCore Nested Subprogram and Access to Globals A subprogram can be nested in any scope A nested subprogram will have access to the parent subprogram parameters, and variables declared before procedure P (V : Integer) is W : Integer; procedure Nested is begin W := V + 1; end Nested; begin W := 0; Nested;

Slide: 15 Copyright © AdaCore Null procedures and expression functions A subprogram with no body can be declared as null (see later for actual usages of this) A function that consists only of the evaluation of an expression can be completed at the specification level as an expression-function This comes in handy when writing e.g. Pre / Post conditions procedure P (V : Integer) is null; function Add (L, R : Integer) return Integer is (L + R);

Slide: 16 Copyright © AdaCore Quiz

Slide: 17 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (1/10) function F (V : Integer) return Integer is begin Put_Line (Integer'Image (V)); return V + 1; end F; begin F (999);

Slide: 18 Copyright © AdaCore NO Is this correct? (1/10) function F (V : Integer) return Integer is begin Put_Line (Integer'Image (V)); return V + 1; end F; begin F (999); F is called, but its return value is not stored

Slide: 19 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (2/10) procedure P (V : Integer) is begin V := V + 1; end P;

Slide: 20 Copyright © AdaCore NO Is this correct? (2/10) procedure P (V : Integer) is begin V := V + 1; end P; Compilation error, V is an input, read-only

Slide: 21 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (3/10) function F () return Integer is begin return 0; end F; V : Integer := F ();

Slide: 22 Copyright © AdaCore NO Is this correct? function F () return Integer is begin return 0; end F; V : Integer := F (); (3/10) Compilation error, parenthesis are not allowed if no parameters

Slide: 23 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (4/10) procedure P (V : Integer) is procedure Nested is begin W := V + 1; end Nested; W : Integer; begin W := 0; Nested;

Slide: 24 Copyright © AdaCore NO Is this correct? (4/10) procedure P (V : Integer) is procedure Nested is begin W := V + 1; end Nested; W : Integer; begin W := 0; Nested; Compilation error, W is not yet visible at this point

Slide: 25 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (5/10) function F return String is begin return "A STRING"; end F; V : String (1.. 2) := F;

Slide: 26 Copyright © AdaCore NO Is this correct? (5/10) function F return String is begin return "A STRING"; end F; V : String (1.. 2) := F; Run-Time error, the size of V and the value return by F do not match

Slide: 27 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (6/10) procedure P (Data : Integer); procedure P (Result : out Integer);

Slide: 28 Copyright © AdaCore NO Is this correct? (6/10) procedure P (Data : Integer); procedure P (Result : out Integer); Parameter names and modes are not significant enough for overload

Slide: 29 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (7/10) procedure P (V : Integer := 0); procedure P (V : Float := 0.0); begin P;

Slide: 30 Copyright © AdaCore NO Is this correct? (7/10) procedure P (V : Integer := 0); procedure P (V : Float := 0.0); begin P; Compilation error, call to P is ambiguous

Slide: 31 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (8/10) procedure Multiply (R : out Integer; V : Integer; Times : Integer) is begin for J in 1.. Times loop R := R + V; end loop; end Multiply; Res : Integer := 0; X : Integer := 10; begin Multiply (Res, X, 50);

Slide: 32 Copyright © AdaCore NO Is this correct? (8/10) procedure Multiply (R : out Integer; V : Integer; Times : Integer) is begin for J in 1.. Times loop R := R + V; end loop; end Multiply; Res : Integer := 0; X : Integer := 10; begin Multiply (Res, X, 50); Erroneous, R is not initialized when entering Multiply

Slide: 33 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (9/10) type My_Int is new Integer; function "=" (L, R : My_Int) return Boolean; function "=" (L, R : My_Int) return Boolean is begin if L < 0 or else R < 0 then return True; else return L = R; end if; end "="; V, W : My_Int := 1; begin if V = W then...

Slide: 34 Copyright © AdaCore NO Is this correct? (9/10) type My_Int is new Integer; function "=" (L, R : My_Int) return Boolean; function "=" (L, R : My_Int) return Boolean is begin if L < 0 or else R < 0 then return True; else return L = R; end if; end "="; V, W : My_Int := 1; begin if V = W then... Infinite recursion on "=" A conversion could fix the problem, e.g. Integer (L) = Integer (R)

Slide: 35 Copyright © AdaCore YES (click on the check icon) NO (click on the error location(s)) Is this correct? (10/10) type Rec is record A, B : Integer; end record; function "=" (L : Rec; I : Float) return Boolean; function "=" (L : Rec; I : Float) return Boolean is... A : Rec; begin if A /= 0.0 then...

Slide: 36 Copyright © AdaCore YES Is this correct? (10/10) type Rec is record A, B : Integer; end record; function "=" (L : Rec; I : Float) return Boolean; function "=" (L : Rec; I : Float) return Boolean is... A : Rec; begin if A /= 0.0 then... OK – the declaration of "=" creates an implicit "/="

Slide: 37 Copyright © AdaCore university.adacore.com