1 / 20

Functions

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.

egandy
Télécharger la présentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions Referring to a name or naming a reference

  2. 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

  3. Picture of a function OUT IN cs113 Gene Itkis

  4. 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

  5. Example (from H) • Absolute value double abs(double x) { if (x>= 0) return x; else return –x; } cs113 Gene Itkis

  6. 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

  7. 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

  8. The void • Type of what is not there • No return • No input void donothingforever(void) { for(;;); } cs113 Gene Itkis

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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

  17. 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

  18. Recursion • Factorial • n! = n * (n-1)! • Binary search • Divide interval • Binary search one sub-interval • Others • Termination conditions!!! cs113 Gene Itkis

  19. Functions: advanced issues

  20. 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

More Related