cs31 lecture 2

# Mpg m g 2gasleft c g 3return gasleft mpg return 11 a

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ed 1.mpg := m / g 2.gasLeft := c – g 3.return gasLeft * mpg return 11 A few observations x New variables can be introduced in the algorithm as New needed needed x You don’t need to use a variable with ‘return’, you can use You an expression an x Informal definition: Informal Variable: an identifier Expression: one of: • • • • variable variable ‘operator’ expression (where ‘operator’ is +, -, /, …) expression ‘operator’ variable number 12 Using an algorithm in another algorithm Algorithm ComputeMilesLeft2: IInput: nput: • m: number of miles since last refuel • g: number of gallons consumed since last refuel • c: the capacity of the gas tank (in gallons) Output: • milesToRefuel: the number of miles before refuel is needed 1.mpg := MPG(m,g) 2.gasLeft := c – g 3.return gasLeft * mpg return 13 Calling another algorithm x Convention: use the algorithm name, and pass all inputs Convention: as arguments in parenthesis as x For the arguments, it can be a variable (its value is being For passed), or directly a numerical value passed), Examples: Examples: • • • foo(12) MilesToRefuel(x, y, z) MilesToRefuel(100, x, 15) x The names of the variables are the names from the caller x The algorithm returns some value: its output 14 Comments on (informal)...
View Full Document

## This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online