ITEC 320 Lecture 4 Sub-Types Functions. Functions / Procedures Review Types Homework Questions?

Slides:



Advertisements
Similar presentations
2/7/2008. >>> Overview * boolean * while * random * tuples.
Advertisements

Programming Languages and Paradigms
ITEC 320 Lecture 3 In-class coding / Arrays. Arrays Review Strings –Advantages / Disadvantages Input –What two methods are used? Conditionals Looping.
Week 8 Arrays Part 2 String & Pointer
ManipulatingPictures-Mod6-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.
CIS Computer Programming Logic
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
ITEC 320 Lecture 5 Scope Exceptions. Scope / Exceptions Review Functions Procedures.
1 CSC103: Introduction to Computer and Programming Lecture No 6.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Methods. 2 A sequence of statements can be packaged together as a unit and re-used. A method is a named unit of re-usable code. modifier returnType methodName(
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
ITEC 320 Lecture 10 Examples. Review Strings –What types are there? –What are the differences? –What should you use where? Homework –Hardest part –Easiest.
Introduction to Computer Programming
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
ITEC 320 Lecture 11 Application Part Deux Look Ahead.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Pascal Programming Today Chapter 11 1 Chapter 11.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Passing Parameters. 2 home back first prev next last What Will I Learn? List the types of parameter modes Create a procedure that passes parameters Identify.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
User-Defined Functions (cont’d) - Reference Parameters.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Intro Programming in C++ Computer Science Dept Va Tech August, 2001 © Barnette ND & McQuain WD 1 Pass-by-Value - default passing mechanism except.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
Fall 2001(c)opyright Brent M. Dingle 2001 Abstract Data Types (ADTs) Brent M. Dingle Texas A&M University Chapter 8 – Sections 2 and 3 (and some from Mastering.
Chapter 4 – C Program Control
EGR 2261 Unit 10 Two-dimensional Arrays
CS 115 Lecture 8 Structured Programming; for loops
Barb Ericson Georgia Institute of Technology Dec 2009
Functions.
Building Java Programs
Chapter 9 Scope, Lifetime, and More on Functions
Building Java Programs
Lecture 11 C Parameters Richard Gesick.
Topics Introduction to File Input and Output
More On Enumeration Types
Building Java Programs
Building Java Programs
CEV208 Computer Programming
Lecture 10: Arrays AP Computer Science Principles
CISC181 Introduction to Computer Science Dr
Method of Classes Chapter 7, page 155 Lecture /4/6.
February , 2009 CSE 113 B.
EPSII 59:006 Spring 2004.
Suggested self-checks: Section 7.11 #1-11
Java Programming Language
Fundamental Programming
Reading from and Writing to Files
Building Java Programs
Topics Introduction to File Input and Output
CSCE 206 Lab Structured Programming in C
Reading from and Writing to Files
Presentation transcript:

ITEC 320 Lecture 4 Sub-Types Functions

Functions / Procedures Review Types Homework Questions?

Functions / Procedures Outline Subtypes / Enumerated types Procedures Functions Problems

Functions / Procedures Compiler tasks Compile time –What errors are caught? Run-time –What errors are caught

Functions / Procedures New types Example type Temperature is range ; type Pressure is range ; t: Temperature; p: Pressure; t := 100; -- valid p := t; -- compile error

Functions / Procedures Enumerated types Map an int to a word procedure enum_demo is type Color is (red, blue, green); c: color := green; begin if c = blue then -- do something end if; c:=0; --What happens here? end enum_demo;

Functions / Procedures Modular Types What do you think this is used for type ClockHourType is mod 12; startTime: ClockHourType := 10; breakTime: ClockHourType := startTime + 2; -- 0 endTime: ClockHourType := startTime + 4; -- 2

Functions / Procedures Floating point Example type Money is delta 0.01 digits 15; cent: Money := 0.01; dollar: Money := 0.0; for i in loop dollar := dollar + cent; end loop; put(float(dollar));

Functions / Procedures In-class Let's write a program in groups: Read a low and a high value to define a range, and then read a sequence of numbers, until eof, and count the number of numbers below, in, and above the range. Calculate the percent in each group. Assume the endpoints of the range are in the range. Output, nicely formatted, the counts and percents.

Functions / Procedures Functions Similar to java methods with a non-void return type Must have a return type Must be defined before use Cannot modify parameters

Functions / Procedures Syntax function sumArray(a: Int_Array) return integer is s: Integer := 0; -- holds the sum begin for i in a'range loop s := s + a(i); end loop; return s; end sumArray;

Functions / Procedures Procedure s No return type allowed Parameters can be changed –In (default) –Out (allows passing back to the caller) –In Out (pass in and out) What about the get procedure’s parameter? Structure charts help

Functions / Procedures Syntax with ada.text_io; use ada.text_io; with ada.integer_text_io; use ada.integer_text_io; procedure inout is procedure vars(a: in Integer; b: in out Integer; c: out Integer) is begin b:= b+a; c:= b+3; end vars; x: Integer; y: Integer; z: Integer; begin x:=2; y:=3; vars(x,y,z); put(x'img & "|" & y'img & "|" & z'img); end inout;

Functions / Procedures Issues Assigning to an in parameter Using an out parameter’s values in the procedure (by default it can be anything) Forgetting to assign a value to an out parameter Can’t fool variables –I.e. passing an in mode variable to another procedure where it is an out mode parameter

Functions / Procedures Problems Write a function/procedure that returns whether or not a String is a palindrome

Functions / Procedures Problems Given n of 1 or more, return the factorial of n, which is n * (n-1) * (n-2) Compute the result recursively (without loops).

Functions / Procedures Summary Functions Procedures