90 likes | 220 Views
In this resource for CIS 335 Fall 2001, Barry Cohen discusses the fundamental concepts of functions in programming. Students are required to complete their first homework, covering readings from sections 3.1 to 3.6, and engage with dynamic examples using playing cards. Key topics include the definition of a function, managing complexity, defensive programming, basic code units, and important terminology such as function signature, parameters, and pre/post conditions. Additionally, it addresses the differences between call-by-value and pass-by-reference parameter passing.
E N D
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? • 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 aType funcName(paramType paramName, paramType2 paramName2) { functionBody; return(value); } Note: no functions in function (in C++) cis 335 Fall 2001 Barry Cohen
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 dieValue = toss(aDie); toss(aDie); Or, in C++ dieValue = aDie.toss(); aDie.toss(); cis 335 Fall 2001 Barry Cohen
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 • 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 • 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