1 / 9

Class 1: Functions

Class 1: Functions. Next week. Finish first homework Read 3.1-3.6 For Sept 27 Page 102, 3.10.2 (1) Playing cards Page 103, 3.10.3(1) Employee Class. What is a function?. Why is a function? Manage complexity Defensive programming Basic unit of code The ‘verb’ – gets something done

wynona
Download Presentation

Class 1: 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. Class 1: Functions

  2. Next week • Finish first homework • Read 3.1-3.6 • For Sept 27 • Page 102, 3.10.2 (1) Playing cards • Page 103, 3.10.3(1) Employee Class cis 335 Fall 2001 Barry Cohen

  3. What is a function? • Why is a function? • Manage complexity • Defensive programming • Basic unit of code • The ‘verb’ – gets something done • Single point of entry • A (return) type • (Often) parameters of given type cis 335 Fall 2001 Barry Cohen

  4. Function format aType funcName(paramType paramName, paramType2 paramName2) { functionBody; return(value); } Note: no functions in function (in C++) cis 335 Fall 2001 Barry Cohen

  5. Some terminology • Function prototype • Type • Name • Parameters • Helps type safety • Function definition • The ‘body’ • What to do • Function signature • Name • Parameters cis 335 Fall 2001 Barry Cohen

  6. Calling a function dieValue = toss(aDie); toss(aDie); Or, in C++ dieValue = aDie.toss(); aDie.toss(); cis 335 Fall 2001 Barry Cohen

  7. Pre- and post conditions • Precondition must be true before a block of code (function, loop) • Postcondition must be true after a block of code (function, loop) • Use asserts#include <assert.h> cis 335 Fall 2001 Barry Cohen

  8. Parameter: Call by value • The ‘default’ type of parameter • Makes a copy of the parameter • Changing the copy doesn’t change the original • What if they have the same name? cis 335 Fall 2001 Barry Cohen

  9. Parameter: Pass by reference • Doesn’t make a copy • Gives function access to the original • Changes are permanent • More efficient • Use const to prevent change • Why pass parameters which shouldn’t be changed? cis 335 Fall 2001 Barry Cohen

More Related