Download presentation
Presentation is loading. Please wait.
Published byDuane Bell Modified over 9 years ago
1
Style 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
2
“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
3
Detour: 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
4
Back 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
5
Returning and void 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!
6
Portrait of a Function as a Prototype “For the love of, 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 type Function name Parameters
7
Using 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
8
Local 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
9
Tracing 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
10
Reminders 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.