class 1 functions n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Class 1: Functions PowerPoint Presentation
Download Presentation
Class 1: Functions

Loading in 2 Seconds...

play fullscreen
1 / 9

Class 1: Functions - PowerPoint PPT Presentation


  • 94 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Class 1: Functions' - wynona


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
next week
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

what is a function
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

function format
Function format

aType funcName(paramType paramName, paramType2 paramName2)

{

functionBody;

return(value);

}

Note: no functions in function (in C++)

cis 335 Fall 2001 Barry Cohen

some terminology
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

calling a function
Calling a function

dieValue = toss(aDie);

toss(aDie);

Or, in C++

dieValue = aDie.toss();

aDie.toss();

cis 335 Fall 2001 Barry Cohen

pre and post conditions
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

parameter call by value
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

parameter pass by reference
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