200 likes | 217 Views
Learn about functions and their benefits, including how to structure and partition larger problems, as well as the difference between call by value and call by reference. Includes examples and discussions on scope, global variables, and recursion.
E N D
Functions Referring to a name or naming a reference
Why? • Structure • Partition a larger problem into smaller more manageable chunks • The partition must be “logical” • A good function can be described with a single simple sentence or phrase • Benefits • Development • Debugging and maintenance • Re-use cs113 Gene Itkis
Picture of a function OUT IN cs113 Gene Itkis
Example c=(a2+b2) a b c = bsroot(a*a + b*b); • Somewhere before that… float bsroot(float x){ …\\ function definition return …;} Input type Output type cs113 Gene Itkis
Example (from H) • Absolute value double abs(double x) { if (x>= 0) return x; else return –x; } cs113 Gene Itkis
Good header comments • Purpose/description • Compute square root • Inputs • x – number to compute the square root of (type: float) • Returns • Square root of x (type: float) • Remarks • Uses binary search method cs113 Gene Itkis
YAE: yet another example (from H) int max(int x, int y) /* receives two integers x,y returns the larger of the two */ { if (x>y) return x; else return y; } cs113 Gene Itkis
The void • Type of what is not there • No return • No input void donothingforever(void) { for(;;); } cs113 Gene Itkis
Functions (C++) vs. “Procedure/Subroutines” (others) • Return • Function returns a value (unless void) • Procedures return nothing • procedure void function • Side effects • Print • Write (e.g. to database), etc. • Parameter value change cs113 Gene Itkis
Call by Value • C++ “default” • E.g. examples above are by value • Input parameters immediately before function call = immediately after function return • Changes to the parameters are visible only inside the function cs113 Gene Itkis
Call by Value: example int x=0, y=10; int func(int a) { a++; return a }; y = func(x); //x=0, y=1 cs113 Gene Itkis
Call by Value: example 2 (from H) Employee Harry; … give_raise(Harry.salary, 25); // Harry should get a 25% raise • Void raise or raise with no value void give_raise(salary s, float by) { s= s*(1+by/100.0); } // Harry’s fooled cs113 Gene Itkis
Call by Reference • Side effect: change of parameter value persists after the function return int x=0, y=10; int func(int& a) { a++; return a }; y = func(x); \\ x=1, y=1 cs113 Gene Itkis
Call by Reference • Reference parameter must be a variable (not an expression) int x=0, y=10; int fV(int a) { a++; return a }; int fR(int& a) { a++; return a }; y = fV(x+1); // This is OK. x=0, y=2 y = fR(x+1); // But this is illegal cs113 Gene Itkis
Scope • Scope of a variable x – where x is defined • From its definition to the end of the block { … float x; … { … int x; … {…} … } … } cs113 Gene Itkis
Global variables • Declared outside function, but used inside { int x=5, y; int addx(int a) {return a+x;} y=addx(2); //y=7 } • Bad style cs113 Gene Itkis
Discussion • Call by value vs. call by reference • Efficiency, style, “testing”; pointers • Stubs and small functions • From pseudo-code to code • Looks like a function, … • rand/srand -a function or an object? • “stateful” cs113 Gene Itkis
Recursion • Factorial • n! = n * (n-1)! • Binary search • Divide interval • Binary search one sub-interval • Others • Termination conditions!!! cs113 Gene Itkis
Other topics covered in class • More scope • Scope of function declaration • Example: double recursion • Two f-ns calling each other from their bodies • Prototypes • Namespaces • Static: persistent variables • Typical mistakes • Forgotten returns • [Pointers to] non-persistent variables • Style: bad side-effects examples • Printing error msgs from a function • Terminating programs from a function • Const ref cs113 Gene Itkis