procedural programming & fundamentals of...

21
18.04.2018 1 Procedural Programming & Fundamentals of Programming Lecture 2 - Summer Semester 2018 Prof. Dr.-Ing. Axel Hunger & Joachim Zumbrägel 2 Practical Training REGISTRATION ends this week on Friday (20 th of April 2018) at 12:00 REGISTRATION under http://ti.uni-due.de/ti/ en/education/teaching/ss18/foppp/lab/registration/index.php Time schedule and group list will be announced Friday (27 th of April 2018) under http://ti.uni-due.de/ti/ en/education/teaching/ss18/foppp/lab/index.php More information during the first exercise next Tuesday (24 th of April 2018) Prof. Dr.-Ing. Axel Hunger

Upload: phungthuy

Post on 04-Jun-2018

245 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

1

Procedural Programming & Fundamentals of ProgrammingLecture 2 - Summer Semester 2018

Prof. Dr.-Ing. Axel Hunger & Joachim Zumbrägel

2

Practical Training

• REGISTRATION ends this week onFriday (20th of April 2018) at 12:00

• REGISTRATION underhttp://ti.uni-due.de/ti/en/education/teaching/ss18/foppp/lab/registration/index.php

• Time schedule and group list will be announced Friday(27th of April 2018) underhttp://ti.uni-due.de/ti/ en/education/teaching/ss18/foppp/lab/index.php

• More information during the first exercisenext Tuesday (24th of April 2018)

Prof. Dr.-Ing. Axel Hunger

Page 2: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

2

3

What we know so far...

• Programming is only one step in program development process

• We use computers to solve problems

• Therefore, we reduce complex problems into more simple parts applying problem-solving strategies

• By algorithms we structure a solution approach into a sequence of instructions to follow for solving a problem

• A computer executes the instructions given in a program one afteranother. This sequence can be manipulated by use of program controlstructures in order to repeat or skip parts of the program.

“a problem well put is half solved” ― John Dewey

4

Flowchart

• A diagram that represents an algorithm

• Illustrates a step-wise solution to a given problem

• Arrows showing flow of control

Prof. Dr.-Ing. Axel Hunger

Notation Description

Activity: a processing step, which can consist of several statements.

Decision: where based on evaluating a condition (commonly a YES‐NO question) the algorithm continues in executing the respective branch.

Input/Output: Information is received or send to user.

Start/End State: Total of values the algorithm starts or ends with.

Page 3: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

3

5

Flowchart for calculating the absolute value

• Calculating the absolute value (or modulus) |n| of a real number n is the non-negative value of n without regard to its sign. The absolute value of a number may be thought of as its distance from zero.

Prof. Dr.-Ing. Axel Hunger

6

In the beginning was the GOTO (1/3) --Example: calculating absolute values in 3AC

Prof. Dr.-Ing. Axel Hunger

Address Content of storage cell Comments

00000 adr 00008 := con ‐13 Begin of program

00001 adr 00009 := con ‐1

00002 if val 00008 > 0 then GOTO adr 00006 Conditional jump (based on decision)

00003 Adr 00008 := val 00008 * val 00009 Simply taking the next instruction the PC points to is one example for an unconditional jump

...

00006 ... next instruction End of program

...

00008 ‐13     13 Storage cell for variable „n“

00009 ‐1 Storage cell for variable „factor“

Page 4: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

4

7

In the beginning was the GOTO (2/3) --How to make use of identifiers

• An identifier (id) is a label.

• In our 3AC example it exchanges the absolute address of a storage cell by a name.

• Identifiers do not change the program but its readability.

Prof. Dr.-Ing. Axel Hunger

Address Content of storage cell

00000 adr 00008 := con ‐13

00001 adr                             := con ‐1

00002 if val 00008 > 0 then GOTO adr 00006

00003 Adr 00008 := val 00008 * val

...

00006 ... next instruction

...

00008 ‐13     13

00009 ‐1

factor

factor

factor

8

In the beginning was the GOTO (3/3)

Prof. Dr.-Ing. Axel Hunger

Address Content of storage cell

START adr number := con ‐13

00001 adr factor := con ‐1

00002 if val number > 0 then GOTO adr END

00003 adr number := val number * val factor

...

END ... next instruction

...

number ‐13     13

factor ‐1

Example for n=‐13

Page 5: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

5

9

Perform according to Program Development Process

Prof. Dr.-Ing. Axel Hunger

Building up a self‐contained model of the problem situation:1. 1.1. Restate the problem by 

collecting all facts1.1.2.  Reduce the problem to 

relevant parts

Transferring model of problem situation into a desired state systematically:1.2.1. Create a step‐wise change of the 

model written as algorithm1.2.2. Document solution approach in 

a commonly accepted way

10

Step-by-Step Solution Design

• Problem-Solving Technique used here: Reduction• Approach we use is comparable to Means-Ends

Analysis (MEA) in which the solution of a problem can be described by finding a sequence of actions that lead to a desirable goal

• Reaching the goal depends on building up associations between particular model state changes and methods that will bring these changes about

• Represented by flowchart

Prof. Dr.-Ing. Axel Hunger

Page 6: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

6

11

Overview of relevant parts for: How to cross the river?

Prof. Dr.-Ing. Axel Hunger

• Attribute / Elements: – Boat– Fox– Goose– Corn

• Values: – Near shore– Far shore

• Methods: – Cross the river– Load– Unload– Safe shore?

12

Creating a flowchart for: How to cross the river?

Prof. Dr.-Ing. Axel Hunger

INIT STATEBoat  @NEARFox  @NEARGoose @NEARCorn  @NEAR

GOAL STATEBoat  @FARFox  @FARGoose @FARCorn  @FAR

?

START

END

Page 7: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

7

13

Creating a flowchart for: How to cross the river (1/5)

Prof. Dr.-Ing. Axel Hunger

CROSS THE RIVER

START

END

CURRENT STATEBoat  @FARFox  @NEARGoose @NEARCorn  @NEAR

We cannot END here,all conditions are violated andnothing has been carried over.

14

Creating a flowchart for : How to cross the river? (2/5)

Prof. Dr.-Ing. Axel Hunger

CROSS THE RIVER

START

END

CURRENT STATEBoat  @FARFox  @FARGoose @NEARCorn  @NEAR

LOAD ITEM

UNLOAD ITEM

We cannot END here,since the farmer has to judge which item is safe to carry over.

Page 8: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

8

15

Flowchart: How to cross the river? (3/5)

Prof. Dr.-Ing. Axel Hunger

CURRENT STATEBoat  @FARFox  @NEARGoose @FARCorn  @NEAR

We cannot END here, because not all items are carried over to the far shore.

SAFE SAFE SHORE

?CROSS THE RIVER

START

END

LOAD ITEM

UNLOAD ITEM

YES NO

16

Creating a flowchart for: How to cross the river? (3/5)

Prof. Dr.-Ing. Axel Hunger

CURRENT STATEBoat  @FARFox  @NEARGoose @FARCorn  @NEAR

We cannot do a simple repetition, otherwise we would undo our advancement towards a solution.

GOAL STATE?

SAFESAFE?

CROSS THE RIVER

START

END

LOAD ITEM

UNLOAD ITEM

YES NO

YES NO

Page 9: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

9

17

Final flowchart for: How to cross the river? (5/5)

Prof. Dr.-Ing. Axel Hunger

CROSS THE RIVER

START

END

GOAL GOAL STATE?

NOYES

LOAD ITEM

UNLOAD ITEM

SAFE?

NOYES

CROSS THE RIVER

SAFE?

CROSS THE RIVER

LOAD ITEM

UNLOAD ITEM

YES NO

Trick: In case of a safe far shore situation return with an empty boat (no items loaded) to near shore, otherwise load critical item and return to near shore.

18

Flowchart and Pseudocode

Prof. Dr.-Ing. Axel Hunger

CROSS THE RIVER

START

END

GOAL GOAL STATE?

NOYES

LOAD ITEM

UNLOAD ITEM

SAFE?

NOYES

CROSS THE RIVER

SAFE?

CROSS THE RIVER

LOAD ITEM

UNLOAD ITEM

YES NO

• Pseudocode is a kind of structured English for describing algorithms.

• It allows to focus on the logic of the algorithm, which describes the sequence of instructions to follow represented by control structures such as:

• Sequence  • Decision 

IF‐THEN‐ELSE• Repetition 

WHILE‐DO, REPEAT‐UNTIL• Procedure

Page 10: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

10

19

Pseudocode for flowchart: How to cross the river?

Prof. Dr.-Ing. Axel Hunger

PROCEDURE RowBoatFromNearToFarShoreLoadItemREPEATLoadItemUNTIL SafeCrossTheRiverUnloadItemENDPROCEDURE

STARTCALL RowBoatFromNeartoFarShoreWHILE NotGoalState DO

CALL RowBoatFromFarToNearShoreCALL RowBoatFromNeartoFarShore

ENDWHILEEND

PROCEDURE RowBoatFromFarToNearShoreIF SafeTHEN CrossTheRiverELSE

LoadItemCrossTheRiverUnloadItem

ENDIFENDPROCEDURE

20

Writing a computer program is only one step in the Program Development Process

Prof. Dr.-Ing. Axel Hunger

Page 11: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

11

21

Pseudocode: How to cross the river?

Prof. Dr.-Ing. Axel Hunger

PROCEDURE RowBoatFromNearToFarShoreREPEATLoadItemUNTIL SafeCrossTheRiverUnloadItemENDPROCEDURE

STARTCALL RowBoatFromNeartoFarShoreWHILE NotGoalState DO

CALL RowBoatFromFarToNearShoreCALL RowBoatFromNeartoFarShore

ENDWHILEEND

PROCEDURE RowBoatFromFarToNearShoreIF SafeTHEN CrossTheRiverELSE

LoadItemCrossTheRiverUnloadItem

ENDIFENDPROCEDURE

22

Use of Procedures in pseudocode

STARTREPEATLoadItemUNTIL SafeCrossTheRiverUnloadItemWHILE NotGoalState DOIF SafeTHEN CrossTheRiverELSELoadItemCrossTheRiverUnloadItem

ENDIFREPEATLoadItemUNTIL SafeCrossTheRiverUnloadItemENDWHILEEND

Pseudocode without use of procedures.

• Procedure contains a sub-sequence of instructions given in the program to be carried out by the computer. It might be called at any point during program execution.

• Procedural programming is based upon the concept of procedure calls.

Prof. Dr.-Ing. Axel Hunger

PROCEDURERowBoatFromNearToFarShore

PROCEDURERowBoatFromNearToFarShore

Page 12: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

12

23

Procedures and Functions (1/2)

• Procedures do calculations without explicitly returning a value

• Empty return type of procedures it indicated by keyword: void

Prof. Dr.-Ing. Axel Hunger

Execution environment

Procedure INPUT OUTPUT

parameter (value change)

Procedure

• contains a subset of a program‘s statements by which it can produce change of values.

24

Procedures and Functions (2/2)

• Functions are also known as function procedures

• Return type of a function can be any available data type

Prof. Dr.-Ing. Axel Hunger

Execution environment

FunctionINPUT OUTPUT

parameter return value

Function

• is a kind of procedure, which calculates a value and returns it                           as a result of calling this procedure.

Page 13: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

13

25

Data and Data Types

• In a computer, data is represented by state of electricity (on=I, off=0).Any kind of data can be represented (in a binary form) by a combinationof Is and 0s.

• We have to choose which combination (bit pattern) represents each piece of data.

• For example, 0I00I0I0I could represent the word „computer“.

Prof. Dr.-Ing. Axel Hunger

• Data is stored in the main memory of the computer.

• The main memory consists of equally sized (and thus in its capacity limited) storage cells, each can be accessed by an individual address.

26

Data Types (1/5)

• The smallest addressable unit of data is usually a group of 8 bits called a byte.

• The unit processed by machine code instructions is called a word (typically 32 or 64 bits).

• Assigning bit pattern to pieces of data is called coding, because the onlylanguage the first computer recognized was binary in form, where programmingmeant translating both data and algorithms in patterns of 1s and 0s.

• Today – its the same process we know as programming, where we translate analgorithm into a programming language.

Prof. Dr.-Ing. Axel Hunger

Binary Number with ... can represent ... different patterns

1 bit 21 (2)8 bits ( = 1 Byte) 28 (256)10 bits 210 (1024)

Page 14: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

14

27

Data Types (2/5)

• Patterns of bits by themselves are meaningless. It is the way the patterns are used that gives them meaning.

• A data type allows for identifying a particular type of data. It determines

– possible values for that type, – their meaning, – the operations that can be done on that values and– how these can be stored.

Prof. Dr.-Ing. Axel Hunger

Data Type

• classifies a set of data (values, which a variable can posses) by defining its characteristics and how to change it.

28

Data Types (3/5)

Prof. Dr.-Ing. Axel Hunger

Integer Boolean

Meaningor type characteristic

whole numbers true or false

(range of) Values

‐2,147,483,648 to 2,147,483,647

I or 0

operations add, sub, mult, mod set, unset

storage 32 bit 1 bit

Page 15: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

15

29

Data Types (5/5)

• The internal representation of data is the way (as a sequence of bits) the value is stored in the main memory of the computer.

Prof. Dr.-Ing. Axel Hunger

Adr. Content of storage cell

222 ... 26 25 24 23 22 21 20

A 0 0 0 0 0 0 0 0 0 0 0 0 0 ... 0 1 1 0 0 1 0 0

B 0 1 0 0 0 0 1 0 1 0 0 1 0 ... 0 0 0 0 0 0 0 01 bit (±)

8 bit(Exponent-127)

23 bit(Mantisse)

... ...

integer A := 100;float B := 72.0;

A B72

100AttributeModel Value...

...as externally represented in programming language

...asrepresentedin model

30

Data Types (4/4)

• Variable: provides temporary storage of data values.

• Data Type: defines the type of data that will be stored in a variable.

• By data types a programmer can decide on the storage for a variable that a compiler should reserve.

• A compiler may use the type of a value to optimize the storage it needs and the choice of algorithms for operations on the value.

• Avoidance of type confusion (add of integer is different to add of float).

• Compilation and execution are two distinct processes

– Compilation translates program to machine language– Execution processes machine language version of program

Prof. Dr.-Ing. Axel Hunger

Page 16: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

16

31

Definition and Declaration of variables

• Every variable must be assigned a data type and given a unique name (identifier).

• Declaration of A: gives information to the compiler about the variable; i.e. its type and its name is announced.

• Definition of A: allocates a memory cell for the variable (e.g. on its initialization).

• It depends on the programming language used to which extent declaration and definition of variables is distinguished.

Prof. Dr.-Ing. Axel Hunger

integer A; Declaration of A

integer A = 100; Definition of A

32

Declaration and Definition of (function) procedures (1/2)

• A procedure introduces a new statement. Making use of this statement is named procedure call.

• Procedures calling themselves are named recursive procedures.

• Declaration of procedure: is the announcement of its signature by naming its head: (return) type, name and a list of parameters

<return_type> <name> ( <input_param_1>,...,<input_param_n> )

Prof. Dr.-Ing. Axel Hunger

PROCEDURE RowBoatFromNearToFarShoreREPEATLoadItemUNTIL SafeCrossTheRiverUnloadItemENDPROCEDURE

void rowBoatFromNearToFarShore(void){...

}

Page 17: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

17

33

Declaration and Definition of procedures

• Definition of (function) procedure is defining its body, which gives theimplementation of procedure.

Prof. Dr.-Ing. Axel Hunger

void absoluteNumber(int n){

if (n>0) printf(„Your number is > O“);elseprintf(„Your number is < O“);

}

head

body

34

Parameter

• There are two kinds of parameter:

• Formal parameter, which represent a special kind of variable used in procedure

• Actual parameter, which are a piece of data provided as input to the procedure

• Parameters are the local variables of a procedure, which are initialized by values given on a procedure call.

Prof. Dr.-Ing. Axel Hunger

void absoluteNumber(int n);int number = -13;int factor = -1;

main(){absoluteNumber(number);}

void absoluteNumber(int n){int number = n;if (number>0)

then; else number = number * factor;

printf(„Your number is: %d\n“,number);}

formal parameter n

actual parameter number

Page 18: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

18

35

Passing parameters –requires knowing which values to use ?

• When calling a procedure, it needs to know which values to use, named actual parameters (or arguments), which can be

• explicitly named values• values which are set by default• implicitly given values from the visible environment

• Since the body of a procedure is a self-contained program block, it is possible that there are several variables with the same name in different procedures (or parts of the program) at the same time.

• How does the computer know, which value to use?

Prof. Dr.-Ing. Axel Hunger

36

Scope of a variable (1/2)

• Scoping prevents procedures from assessing the variables of otherprocedures and vice versa.

• The scope of a variable is the range of statements that can assess it. Itis the environment in which the variable is available.

• Within the same scope it is not possible to declare two variables of thesame name and type.

• Definitions assign values to an area of memory that was reservedduring declaration.

Prof. Dr.-Ing. Axel Hunger

Page 19: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

19

37

Scope of a variable (2/2)

Prof. Dr.-Ing. Axel Hunger

void absoluteNumber(int n);int number = -13;Int factor = -1;

main(){absoluteNumber(number);}

void absoluteNumber(int n){int number = n;if (number>0)

then; else number = number * factor;

printf(„Your number is: %d\n“,number);}

Global environmentWith variable number

Local environmentWith variable number,which scope belongs to the body of procedure absoluteNumber

Address Content of storage cell

number ‐13

...

number ‐13     13

38

Visibility & Lifetime of a variable (1/2)

• A variable is visible within its scope or hidden outside it.

• Visibility arises because of the possibility that a variabe in outer scopehas the same name as a variable in inner scope resulting in ‚hidding‘.

• Visibility refers to the accessibility of a declared variable.

• Lifetime of a variable is the time in which storage is bound to variable orduration in which the variable is ‚alive‘.

• Lifetime and visibility of a variable to not have to be the same

Prof. Dr.-Ing. Axel Hunger

Page 20: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

20

39

Visibility & Lifetime of a variable (2/2)

Prof. Dr.-Ing. Axel Hunger

START1. void absoluteNumber(int n);2. int number = -13;3. Int factor = -1;

4. main(){5. absoluteNumber(number);}11.END

void absoluteNumber(int n){6. int number = n;7. if (number>0)8. then; 9. else number = number * factor;10. printf(„Your number is: %d\n“,number);}

outer scope

inner scope

variable scope visible lifetime

number outer 2.‐4. and 11. 2.‐11.

number inner 6 to 10 6 to 10

40

Passing parameters (continuation)

• Scope is of relevance during compilation (allocation), lifetime is of relevance during program execution(access).

• Same holds for procedures

Prof. Dr.-Ing. Axel Hunger

Page 21: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture2.pdf · Fundamentals of Programming Lecture 2 - Summer Semester 2018

18.04.2018

21

ENDof 2nd Lecture