Week 3 Notes - Chapter 5 - IF, FUNCTION, PACKAGE

Some Follow-up From Last Week

Las week we talked about the Screen package. There is a little difference between the way it's presented in the book and the way it's implemented on the lab server. Specifically, the Screen.MoveCursor() function takes different parameters. The following source code file illustrates how to do it:

Theory

Types of Programming Statements

Basically, there are three kinds of programming statements:

Sequential: This means that statements come one after another in the execution of a program. This is the simplest form.

Conditional: Here, you have a block of statements which are executed depending on whether some condition is true or false. The best example is your classic 'if' statement. Said 'if' statement can be spiced up with the addition of 'else' or even 'else if' clauses. Furthermore, the logic can be reversed via an 'unless' identifier.

Looping: This is a block of (sequential) statements which are executed over and over for a predetermined number of times. The best example is the classic 'while' statement (and it's close cousin 'until', which is simply a while with the logic reversed). The condition can be tested at the end of a block (ensuring that the block is executed at least once) via a 'do-while' or 'do-until' construct.

The well-known 'for' (or 'foreach') statment is also a looping construct, but optomized for a different purpose: Iterating through items in a list, array, or sequence. There is still an exit condition in a for loop: The end of the list.

More Theory: Approaches to Statement Block Syntax

As mentioned in week1, the big selling point of structured programming is the division of a program into blocks of statements. The different programming languages have various syntactic methods of indicating those blocks. While there is no One True Way to indicate blocks, there are some constructs which are syntactically "cleaner" than others.

Compare and contrast:

At the end of the day, none of this is really, really critical, but syntax layout can have a significant influence on the readability of the code. As mentioned before, programming should not be a religious practice; everybody has their own coding style and no one should decree anyone else's indentation style as a capital offense, just because it is different from your own. Likewise, if you are involved in a large project which is being worked on by many people, you should be willing to conform to the coding style that others are using to make life easier on everyone. Too, too much blood has been spilt over indentation styles already, and no more needs to be spilt. One nice thing about Ada, is that it doesn't afford a whole lot of variety, so you don't have to get swept up into these arguments.

Now for the Ada Stuff

The IF Statement

The fun starts on page 154. Page 161 has a good synopsis. Here's how an IF statement looks:

IF x < 3 THEN    -- test condition
	stmt     -- if condition is true
END IF;
IF x < 3 THEN    -- test condition
	stmt     -- if condition is true
ELSE                -- (ELSE clause is optional)
	stmt     -- if condition is false
END IF;

Page 175 shows how to do "else if" clauses. Here's how it looks: (Note that there is only one 'E' in ELSIF.)

IF x < 3 THE      -- test condition #1
	stmt      -- if condition is true
ELSIF x > 3 THEN  -- test condition #2 (ELSIF clause optional)
	stmt      -- elsif condition is true
ELSE                 -- ELSE clause also optional
	stmt      -- both previous conditions are false
END IF;

The FUNCTION Statement

You've already learned how to write procedures. In Ada, procedures do not return a value, functions do. The section on functions starts on page 183.

Function specification, p 186: This would go in a .ads file -- the Ada equivalent of a .h file in C.

FUNCTION fname ( parms ) RETURNS type

Function Body, page 187.

FUNCTION fname ( parms ) RETURN type IS
	local-variables 
BEGIN
	stmts
	RETURN result
END fname 

Note also that we learn on p. 185-186 that procedures can contain nested functions, just like in Pascal.

The PACKAGE Statement

Page 188 begins describing how packages work in Ada.

Package spec, page 190. This is the thing that would go in a .ads file.

PACKAGE pname IS
	function-decls
	procedure-decls
	type/subtype-decls
END pname

Package body, also page 190. This is the thing that goes in your .adb file.

PACKAGE BODY pname IS
	function-impls
	procedure-impls
	type/subtype-impls
	(constant) variables
END pname

Note the presence of the keyword BODY in the implementation.

Assignment 3

Write a program that computes sales tax based on an input value. If the value is below $100, the sales tax is 3%. If the price is $100 or more, the sales tax is 6%. Make sure you use a function in adition to your main procedure. You do not have to put your function in a seperate package. In fact, I would prefer that you didn't.

This assignment is due the night of the first midterm, September 26th.