220 likes | 349 Views
Introduction to Algorithmic Processes CMPSC 201C Fall 2000. Administrative Issues. Exam 2 - Monday, November 6 at 8:15 to 9:30 in 102 and 105 Forum Sections 3, 4, 5, 6, 9,and 10 (John and Pyush) will be in 102 Forum and sections 1, 2, 7, 8, 11,and 12 (Anand and Nidhi) will be in 105 Forum.
E N D
Administrative Issues • Exam 2 - Monday, November 6 at 8:15 to 9:30 in 102 and 105 Forum • Sections 3, 4, 5, 6, 9,and 10 (John and Pyush) will be in 102 Forum and sections 1, 2, 7, 8, 11,and 12 (Anand and Nidhi) will be in 105 Forum. • Conflict form must be returned to me by noon on October 25! • Two practice exams will be on web and I-drive by October 25.
Types of parameters • Formal parameters(arguments) - parameters listed in the function heading • Actual parameters(arguments) - parameters listed in the function call
Example Cont. //Function to calculate miles per gallon for a tank of gas……. // int mpg(double oldOdom, double newOdom, double gallons) // function heading { int ans = (int) ((newOdom - oldOdom)/gallons)); return ans; }
Scope • Scope - portion of code in which the identifier name is accessible. • Local - limited to a specific block of statements in which the identifier was declared (or defined for a function name) • Global - may be accessed from any portion of code once the identifier has been declared (usually at beginning of code). • Usually variables are local and constants are global. • If you do use global variables, be VERY careful as they can be affected in ways you did not expect.
Block • Statements that are enclosed braces { }. • May be body of function, loop, if-then, etc.
Scope Rules • A name that is declared within a block is local to that block and is accessible only within that block. • All other names are “inherited” from the immediate surrounding region.
Lifetime • Lifetime of a variable or constant is the time when memory has been allocated to it. • If the constant or variable is global, memory is always allocated to that constant or variable and the lifetime would be static. • If the constant or variable is local, then memory is only allocated when the block in which it was declared is executing and the memory is deallocated when the block is finished executing. In this case the lifetime would be automatic.
Multiple Results • Value- returning function returns one value to the function that called it. • However, sometimes more that one result is calculated and need to be passed back to the originating function. • Then use reference parameters (arguments) in the formal parameter list.
Example (pg. 149) // Add two vectors that are at right angles….. // Pi is a global CONSTANT that was declared before // main void addVect (double a, double b //vectors at rt <‘s double& rMagnitude // output int& rDirection ) // output - direction { double rDirRadians; //local variable rMagnitude = sgrt(a * a + b * b); rDirRadians = atan (b / a); rDirection = (int) (180 / Pi * rDirRadians + 0.5); }
Function Heading • Pass by value int mpg(double oldOdom, double newOdom, double gallons) • Pass by reference void addVect (double a, double b //vectors at rt <‘s double& rMagnitude // output int& rDirection ) // output - direction
Function Prototype • Pass by value int mpg (double, double, double); • Pass by reference void addVect (double, double, double&, int&);
Function call • Pass by value lastTankMPG = mpg(10502.5, 10754.6, 10.0); • Pass by reference addVect (aVect, bVect, rMag, rDir);
Pass by reference • When using pass by reference, both identifiers (the one in the function call and the one in the function heading) refer to the samememory location. • Therefore, when formal parameter (the one in the function) changes what is stored in the memory location, the same change occurs for the actual parameter
Formal and Actual Parameters Formal ParameterActual Parameter Pass by value value, variable, or an expression Pass by reference variable
Value vs Nonvalue • Use value-returning functions (int, float, double, etc.) when one value needs to be passed back to the calling function. • Use nonvalue-returning functions (void) for printouts, pass multiple results, modify actual parameters. • See table 5.2 on page 153
Recursive Function • A function that calls itself or is part of a cycle of calls. f1 f1 f3 f2
Non-programming Example • Recursive definition of a cow. • A cow is a four-legged animal whose mother was a cow.