Lecture 02

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Lecture 2 9/6/07 Algorithms and Programs Quote of the Day The people may be made to follow a path of action but they may not be made to understand it. Confucius How are algorithms invoked? Every algorithm has a caller. An algorithm is usually called by another algorithm. Caller Algorithm A How is data handled? Data is exchanged between the algorithm and its caller. Since our definition of an algorithm is that it transforms input to output there must be a way to accept input and provide output to the caller. Caller Data Data Data Algorithm A Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x y do Assign xy to x Return x End Caller x y Algorithm A Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x y do Assign xy to x Return x End Caller 20 x y Algorithm A Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x y do Assign xy to x Return x End Caller 20 7 x y Algorithm A Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x y do Assign xy to x Return x End Caller 13 7 x y Algorithm A Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x y do Assign xy to x Return x End Caller 6 7 x y Algorithm A Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x y do Assign xy to x Return x End Caller x y Algorithm A Example: Remainder Algorithm 1. 2. 3. 4. 5. 6. Request a value for x Request a value for y While x y do Assign xy to x Return x End Caller x output 6 y Algorithm A How is data sent to/from the caller? Passbyvalue copies the data from the caller to a new location in the algorithm. When passbyvalue is used to send data back to the caller, this is called a return. The caller has the option to ignore this kind of data. Passbyreference gives direct access to the data in the caller's environment. The caller cannot ignore passbyreference data send to it since the dat...
View Full Document

Ask a homework question - tutors are online