1 / 27

Imperative Languages: Parameter Passing, Modules, and Program Composition

Learn about parameter association, default parameters, procedures as parameters, modules, and program hierarchy in imperative languages. Explore the concepts of positional and named parameter association, default parameters, and passing procedures as parameters. Understand the structure and usage of modules in Ada and C, and learn how to create and use C header files.

cfisk
Download Presentation

Imperative Languages: Parameter Passing, Modules, and Program Composition

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. COMP205 IMPERATIVE LANGUAGES 14. PROGRAM COMPOSITION II 1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header files 3) Programs 4) Generics and Abstract Data Types (ADTs)

  2. PROGRAM HIERARCHY • Four levels of hierarchy can be distinguished: 1) Blocks. 2) Sub-programs (procedures and functions). 3) Modules, packages and tasks. 4) Programs.

  3. PARAMETER ASSOCIATION • The normal procedure when calling a routine is to list all the actual parameters in order separated by commas. • This is called positional parameter association. • This means that any actual parameters supplied must agree with the formal parameters in number, order and type. • Most imperative languages (Ada and C included) support positional parameter association. • The alternative is keywordor named association.

  4. procedure POS_PARAM_ASSOC is -- ADD_UP procedure procedure ADD_UP(X, Y, Z : INTEGER) is begin PUT(X+Y+Z); NEW_LINE; end ADD_UP; -- Top level begin ADD_UP(2,4,6); end POS_PARAM_ASSOC; POSITIONAL PARAMETER ASSOCIATION

  5. procedure NAMED_PARAM_ASSOC is ------------------------------------- procedure ADD_UP(X, Y, Z : INTEGER) is begin PUT(X+Y+Z); NEW_LINE; end ADD_UP; ------------------------------------- begin ADD_UP(X=>2, Y=>4, Z=>6); ADD_UP(Z=>6, X=>2, Y=>4); ADD_UP(2, Z=>6, Y=>4); end NAMED_PARAM_ASSOC; NAMED PARAMETER ASSOCIATION

  6. NAMED PARAMETER ASSOCIATION • Note that the parameters do not need to be ordered in a particular way. • Note also that we can mix positional and named parameter association provided that the positional parameters precede the named parameters. • The claimed advantage is that this gives a clearer reading of the code. • Anybody reading the code does not need to know exactly the formal parameters used or the order in which they appear before being able to understand the significance of the call.

  7. DEFAULT PARAMETERS • In some language parameters may be given default values. • For example Ada in parameters may be given a default values when the formal parameter is specified. • in out parameters and out parameters may not have default values.

  8. procedure EXAMPLE is N1: float:= 4.2; N2: float:= 9.6; --------------------------------------- procedure MEAN_VALUE (X1: in out float; X2: in float := 6.4) is begin X1:= (X1+X2)/2.0; end MEAN_VALUE; --------------------------------------- begin MEAN_VALUE(N1); put(N1); new_line; MEAN_VALUE(N1, N2); put(N1); new_line; end EXAMPLE; $ example 5.3 7.4

  9. PROCEDURES AS PARAMETERS • Foregoing all assumes that we wish to pass data items. Some languages support passing of procedures (e.g. Pascal). • Example: in numerical analysis, where methods for finding (say) roots of functions etc. can be written to be independent of any particular routine, it is useful to be able to pass appropriate functions to solving routine.

  10. MODULES • Overview • Creating and using a package (in Ada) • Creating and using a C module • Creating and using a C header (.h) file.

  11. MODULES • A number of related declarations of types, variables and routines can be grouped together into a module or package. • The concept of modules (and modular programming) appeared in the 1970's in response to the increasing size of programs (in Modula-2 everything is a module).

  12. MODULES • Modules comprise a specification part and an implementation part. • The specification part (or definition module) contains a description of the interface and the implementation part the necessary code. • To use a module the programmer must "know about" the specification part, but not necessarily the implementation part. • We say the implementation part is hidden (information hiding).

  13. CREATING AN ADA PACKAGE package TIMESTWO is -- Specification function TIMES_TWO ( X : integer ) return integer; end TIMESTWO ; -- Body package body TIMESTWO is function TIMES_TWO ( X : integer ) return integer is begin return(X*2); end ; end TIMESTWO ; Ada packages comprise two parts:1) A specification part which gives the user information about the resources contained and how they are used.2) A package body that details the resourcesNote that the body is not visible to the user.

  14. with CS_IO, TIMESTWO; use CS_IO, TIMESTWO ; procedure EXAMPLE is S: integer:= 4; T: integer:= 0 ; begin T := TIMES_TWO(S); put("S = "); put(S); new_line; put("T = "); put(T); new_line; end EXAMPLE ; Compilation:1)ada timestwo.adaada example.adaada -link example2)ICC timestwo.adaICC example.ada

  15. /* SPECIFICATION */ int timesTwo(int); /* BODY */ int timesTwo(int x) { return(x*2); } CREATING A C MODULE #include <stdio.h> /* Prototypes */ external int timesTwo(int); /* Code */ void main(void) { int x=4; printf("Two times %d = %d\n", x, timesTwo(x)); } Compilation:cc -Aa -c timestwo.ccc -Aa -c example.ccc -Aa -o example example.o timestwo.o

  16. CREATING A C HEADER FILE • Whereas a C module is first compiled to create an object file (.o) and later linked into the "main" program, a header file is “compiled in” at the same time as the "main" program. • Procedure is as follows, first create a .h file: /* SPECIFICATION */ int timesTwo(int); /* BODY */ int timesTwo(int x) { return(x*2); }

  17. Include this in code as follows: #include <stdio.h> #include "timestwo.h" void main(void) { int x=4; printf("Two times %d = %d\n", x,timesTwo(x)); }

  18. PROGRAMS

  19. PROGRAMS • The concept of programs has existed since the conception of computer programming. • With respect to C and Ada a program consists of one or more object files linked together. • Note that individual object files are not independent. • The main issues in program composition from object files are: 1) How to indicate which objects are part of the desired program --- linking 2) Providing an answer to the question “where do we start processing from?”.

  20. SPECIFYING PROGRAMCOMPOSITION IN ADA • Ada assumes that there exists a method outside the language which allows the programmer to specify the name of a procedure and which will then construct an executable binary program for that procedure containing all the necessary modules.

  21. SPECIFYING PROGRAMCOMPOSITION IN C • C assumes that the programmer will specify all necessary user object files in a specialised invocation of the system linker. • This invocation searches the object files for a routine with the fixed external name main and constructs an executable binary program which, when called, will execute the routine main.

  22. GENERICS • Much of the code written by programmers is very specific. • For example any algorithm used to manipulate linked lists also specifies the type of the items in the list. • It is desirable to be able to write a general algorithm applicable to linked lists of any type. • This is achieved using a construct known as a generic. • A generic “unit” can be considered to be a template from which actual units can be created at compile time. • Ada supports generics, C does not.

  23. Consider the following Ada procedure: procedure SWAP (VAL_1, VAL_2 : in out INTEGER) is TEMP : INTEGER ; begin TEMP := VAL_1; VAL_1 := Val_2; VAL_2 := TEMP; end SWAP; • This "swaps" two integer values. • If we wanted to swap to floating point values or two characters we would require two further procedures. • Alternatively we can use a generic procedure.

  24. Generic "swap" procedure: -- Specification of generic parameters generic typeELEMENTis private; procedure SWAP (VAL_1, VAL_2 : in outELEMENT); -- Body procedure SWAP (VAL_1, VAL_2 : in out ELEMENT) is TEMP : ELEMENT; begin TEMP := VAL_1; VAL_1 := Val_2; VAL_2 := TEMP; end SWAP;

  25. This must now be compiled before it can be used. withSWAP, CS_IO; use CS_IO; procedure SWAP_THINGS is procedureSWAP_INTEGERis newSWAP(INTEGER); procedureSWAP_FLOATis newSWAP(FLOAT); INT_1, INT_2 : INTEGER; FLOAT_1, FLOAT_2 : FLOAT; begin INT_1 := 2; INT_2 := 4; SWAP_INTEGER(INT_1,INT_2); FLOAT_1 := 2.22; FLOAT_2 := 4.44; SWAP_FLOAT(FLOAT_1,FLOAT_2); end SWAP_THINGS;

  26. ABSTRACT DATA TYPES • A data type defines a set of values and a set of operations that may be performed on those values. • An abstract data type (ADT) links a type definition with the operations that can be applied to it. • Values associated with an ADT can only be created and manipulated through the defined operations. • The advantages offered by ADTs concern: • "information hiding" and • "software reuse". • ADTs are an important concept in Object Oriented Programming and Functional Programming....

  27. SUMMARY 1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header files 3) Programs 4) Generics and Abstract Data Types (ADTs)

More Related