1) More on parameter passing: a) Parameter association b) Default parameters c) Procedures as parameters 2) Modules: Ada packages, C modules, C header.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

User Defined Functions
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.
Programming Languages and Paradigms
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
1 Chapter Three Using Methods. 2 Objectives Learn how to write methods with no arguments and no return value Learn about implementation hiding and how.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
1 ) Data Attributes 2) Declarations, Assignment and Instantiation 3) Global and Local Data 4) Variables 5) Constants 6) Example programs COMP205 IMPERATIVE.
1) Data Review 2) Anonymous Data Items 3) Renaming 4) Overloading 5) Introduction to Data Types 6) Basic types 7) Range and precision 8) Ada/Pascal “attributes”
CS 201 Functions Debzani Deb.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1) Causes of errors 2) Classification of errors 3) Signals and exceptions 1) Program hierarchy 2) Blocks 3) Routines 4) Procedures and functions COMP205.
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
© 2003 G. Drew Kessler and William M. Pottenger1 Subroutines (Part 1) CSE 262, Spring 2003.
Object Oriented Software Development
Classes Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd Spetember 2006.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter Nine: Subprograms Lesson 09. What are they  Modularized code  Might return a value  Functions  Or not  Procedures  Subroutines  In object.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CPS120: Introduction to Computer Science Functions.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Programming Languages and Paradigms Imperative Programming.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
SOFTWARE DESIGN. INTRODUCTION There are 3 distinct types of activities in design 1.External design 2.Architectural design 3.Detailed design Architectural.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
Chapter 10, Slide 1 ABSTRACT DATA TYPES Based on the fundamental concept of ABSTRACTION:  process abstraction  data abstraction Both provide:  information.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
Chapter 3: User-Defined Functions I
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
Classes, Interfaces and Packages
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
CLASSES AND OBJECTS Chapter 3 : constructor, Separate files, validating data.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
TK1924 Program Design & Problem Solving Session 2011/2012
Chapter 6: User-Defined Functions I
Abstract Data Types and Encapsulation Concepts
11.1 The Concept of Abstraction
CS212: Object Oriented Analysis and Design
Abstract Data Types and Encapsulation Concepts
User Defined Functions
Abstract Data Types and Encapsulation Concepts
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Introduction to Classes and Objects
Classes and Objects Systems Programming.
11.1 The Concept of Abstraction
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

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) COMP205 IMPERATIVE LANGUAGES 14. PROGRAM COMPOSITION II

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

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 keyword or named association. PARAMETER ASSOCIATION

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

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

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. NAMED PARAMETER ASSOCIATION

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. DEFAULT PARAMETERS

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

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. PROCEDURES AS PARAMETERS

Overview Creating and using a package (in Ada) Creating and using a C module Creating and using a C header (.h ) file. 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). 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). MODULES

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 ; CREATING AN ADA PACKAGE 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 resources Note that the body is not visible to the user.

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.ada ada example.ada ada -link example 2) ICC timestwo.ada ICC example.ada

/* SPECIFICATION */ int timesTwo(int); /* BODY */ int timesTwo(int x) { return(x*2); } CREATING A C MODULE #include /* 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.c cc -Aa -c example.c cc -Aa -o example example.o timestwo.o

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: CREATING A C HEADER FILE /* SPECIFICATION */ int timesTwo(int); /* BODY */ int timesTwo(int x) { return(x*2); }

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

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?”. PROGRAMS

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. SPECIFYING PROGRAM COMPOSITION IN ADA

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. SPECIFYING PROGRAM COMPOSITION IN C

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. GENERICS

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.

Generic "swap" procedure: -- Specification of generic parameters generic type ELEMENT is private; procedure SWAP (VAL_1, VAL_2 : in out ELEMENT); -- 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;

This must now be compiled before it can be used. with SWAP, CS_IO; use CS_IO; procedure SWAP_THINGS is procedure SWAP_INTEGER is new SWAP(INTEGER); procedure SWAP_FLOAT is new SWAP(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;

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.... ABSTRACT DATA TYPES

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)