Style Comments About HW 1 Use #define !!! –All of you already know the reasons for using symbolic...
-
Upload
duane-bell -
Category
Documents
-
view
218 -
download
3
Transcript of Style Comments About HW 1 Use #define !!! –All of you already know the reasons for using symbolic...
Style Comments About HW 1Style Comments About HW 1• Use #define!!!
– All of you already know the reasons for using symbolic constants …but most of you didn’t use #define in HW1
– 305,332* people a year die in car accidents because they didn’t use their seatbelts, even though they the reasons for seat belts …
– You are all still alive, which implies that you can learn from others’ mistakes
• Variables and #define constants– #define constants should be in ALL_CAPS– Choose a naming convention – and follow it!! – Two common naming conventions:
• variable_names_seperated_by_underscores• variableNamesSeperatedByMixedCaps
• Indentation– Alt-F8 re-indents highlighted code in MSVC– Ctrl-a highlights your entire program
* 46.4% of statistics are made up on the spot
““Verify Correctness”Verify Correctness”• Just because your code compiles (ie - is syntactically
correct) doesn’t mean it works properly• 20%* of the time spent in software development is in
writing code; the rest is in correctness verification and debugging
• Test your program– printf intermediate values– Keep a calculator close at hand– Consistent indentation may help reveal logic errors– The order of your input and output is important!– Check for rounding errors
• Questions about the homework?– Office hours or email!
*This is a real statistic
Detour: CookingDetour: Cooking• What is a recipe?
– A recipe is a sequence of instructions which must be executed in order• Ever tried baking a cake without breaking the eggs first?
– A recipe may have a list of ingredients
– A recipe may have a final product • E.g. a recipe for a birthday cake produces a cake (duh!)
– The recipe tells you what to do - but you don’t actually bake a cake until someone asks you to
– A recipe refers to ingredients in ways that may not match the particular ingredients you actually use.
• E.g. - a recipe may tell you to mix “flour” and “sugar” together, but that’s not the same as when you physically mix SoftaSilk flour with C&H granulated sugar
– When one recipe calls for another, the entirety of the other recipe is finished before the first recipe can continue
• Ever tried frosting a cake before baking the layers?
“e.g.” == “exampla gratia” == “a free example” in Latin
Back on Task: FunctionsBack on Task: Functions• What is a function?
– A function is a series of statements executed in sequential order
– A function has a list of parameters, which may be void– A function has a return value, which may also be void
– A function defines behavior - but isn’t actually executed until it is called
– Arguments are the actual data that a function is given. Parameters are names a function uses to refer to the arguments, but they are not one and the same
– When one function calls another, the entirety of the other function is executed before the caller continues execution
Single-letter variable names rarely make sense
Returning and Returning and voidvoid• When a function returns, it ceases execution and
control returns to its caller– The caller resumes execution at the point where the
function was called
• A function returns a value to its caller– The type of the returned value is the return type of the
function
• A function whose return type is void does not return a value
• A function which takes a void parameter list is called with no arguments at all
Follow a consistent naming convention!
Portrait of a Function as a PrototypePortrait of a Function as a Prototype
“For the love of <your favourite deity >, use #define’s!!” - Dutch
• A function declaration/prototype consists of:– Return type– Function name– List of parameters (not arguments) in parentheses
• Function parameters must specify the name and the type• Functions that take no parameters must have the keyword void in place of the parameter list
• A function call is the actual invocation/execution of the function. It consists of :– Function name– List of arguments (not parameters) in parentheses
• Must match the function parameters in number, type, and order• Arguments can be any expression of the appropriate type• Arguments passed to a function do not include a type• Functions with void parameters are called with empty parentheses: ()
int calculateDiscriminant(int a, int b, int c);
Return typeFunction name Parameters
Using and Definining FunctionsUsing and Definining Functions
Function Definition
int calculateDiscriminant(int a, int b, int c) {
int result;result = b*b - 4*a*c;return result;
}
Function Calls
int discr;int first, second, third;
discr = calculateDiscriminant(1, 2, 3);
printf(“The answer is %d”, calculateDiscriminant(1, 2, 3));
discr = calculateDiscriminant(first,
second*2, third-second);
Indent one additional level for each condition, loop, or function
Local Variables and ScopeLocal Variables and Scope• Local variables
– Variables declared inside a function are local – they may have the same name as other variables in other functions, but changing a local variable does not change variables by the same name in other functions
• More cooking analogies:– The “flour” you used when baking a cake is not the
same “flour” used to make Play-doh
• Parameters && Arguments– The values of arguments are copied or substituted into
your functions’ parameters
#define’s should be in ALL_CAPS
Tracing Function CallsTracing Function Calls
int calculateDiscriminant(int a, int b, int c) {
int result;
result = b*b - 4*a*c;
return result;
}
double findRoot(int a, int b, int c) {
double posRoot;
int discr = calculateDiscriminant(a, b,
c);
if(discr < 0)
return -1;
posRoot = (-b + sqrt(discr)) / 2*a;
}
int main(void) {
int a, b, c;
double root;
printf(“Enter the coefficients:”);
scanf(“%d %d %d”, &a, &b, &c);
root = findRoot(a, b, c);
printf(“One of the roots is: %f”, root);
return 0;
}
All functions should have a comment header
RemindersReminders• The midterm covers:
– Chapters 1, 2, 3, and 4, except sections 2.7 && 4.8
• There is a review session today at 5 PM, Johnson 006 (basement)
• There is a midterm on Friday– Bring picture ID
• Homework 2A due this Sunday, 10 PM
Break up complicated calculations into several intermediate steps
“Functions are neat and pretty and I like em.” - Dutch