1 / 75

CS16 Week 3: Constants, Errors, and Functions Overview

This overview covers the topics of constants, errors, and functions in CS16, including defining and using constants, types of errors, and the concept of divide and conquer in problem-solving.

morganf
Download Presentation

CS16 Week 3: Constants, Errors, and Functions Overview

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. Week 3 Part I • Kyle Dewey

  2. Overview • Odds & Ends • Constants • Errors • Functions • Expressions versus statements

  3. Pre-Lab

  4. Underflow & Overflow Note

  5. Constants

  6. Constants • Values which never change • Specific values are constants • 55 • 27.2 • ‘a’ • “foobar”

  7. Constants • Specifically in the program text • Constant in that 52 always holds the same value • We cannot redefine 52 to be 53

  8. Symbolic Constants • Usually when programmers say “constant”, they mean “symbolic constant” • Values that never change, but referred to using some symbol • i.e. π (pi - 3.14...) • Mapping between symbol and value is explicitly defined somewhere

  9. In C • Use #define • By convention, constants should be entirely in caps #define PI 3.14 ... int x = PI * 5;

  10. Mutability • Constants are, well, constant! • Cannot be changed while code runs #define PI 3.14 ... PI = 4; // not valid C!

  11. What #define Does • Defines a text substitution for the provided symbol • This text is replaced during compilation by the C preprocessor (cpp)

  12. Example #1 #define PI 3.14 ... int x = PI * 5; Code After Preprocessor int x = 3.14 * 5;

  13. Example #2 #define PI 3.14 ... PI = 4; Code After Preprocessor 3.14 = 4;

  14. Best Practices • Generally, all constants should be made symbolic • Easier to change if needed later on • Gives more semantic meaning (i.e. PI is more informative than 3.14...) • Possibly less typing

  15. Errors

  16. Errors • Generally, expected result does not match actual result • Four kinds of errors are relevant to CS16: • Syntax errors • Linker errors • Runtime errors • Logic errors

  17. Errors • Four kinds of errors are relevant to CS16: • Syntax errors • Linker errors • Runtime errors • Logic errors

  18. Syntax Error • A “sentence” was formed that does not exist in the language • For example, “Be an awesome program” isn’t valid C

  19. Syntax Error • Easiest to correct • Compiler will not allow it • *Usually* it will say where it is exactly

  20. On Syntax Errors • ...sometimes the compiler is really bad at figuring out where the error is #include <stdio.h>int main() { printf( "moo" ) printf( "cow" ); return 0;}

  21. Reality #include <stdio.h>int main() { printf( "moo" ) printf( "cow" ); return 0;} • Missing semicolon at line 4

  22. GCC #include <stdio.h>int main() { printf( "moo" ) printf( "cow" ); return 0;} syntax.c: In function ‘main’:syntax.c:5: error: expected ‘;’ before ‘printf’

  23. Ch #include <stdio.h>int main() { printf( "moo" ) printf( "cow" ); return 0;} ERROR: multiple operands together ERROR: syntax error before or at line 5 in file syntax.c ==>: printf( "cow" ); BUG: printf( "cow" )<== ???

  24. The Point • Compilers are just other programs • Programs can be wrong • Programs are not as smart as people

  25. Errors • Four kinds of errors are relevant to CS16: • Syntax errors • Linker errors • Runtime errors • Logic errors

  26. Recall Linking 1: somethingFromHere(); 2: somethingFromElsewhere(); 3: somethingElseFromHere(); somethingFromHere somethingFromElsewhere somethingElseFromHere

  27. Recall Linking somethingFromElsewhere somethingFromHere somethingElseFromHere

  28. Linker Errors • What if somethingFromElsewhere is nowhere to be found? • Missing a piece • Cannot make the executable

  29. Example • int something(); tells the compiler that something exists somewhere, but it does not actually give something int something();int main() { something(); return 0;}

  30. Example int something();int main() { something(); return 0;} Undefined symbols for architecture x86_64: "_something", referenced from: _main in ccM6c8aW.old: symbol(s) not found for architecture x86_64

  31. Errors • Four kinds of errors are relevant to CS16: • Syntax errors • Linker errors • Runtime errors • Logic errors

  32. Runtime Errors • Error that occurs while the code is running • Compilation and linking must have succeeded to get to this point

  33. Examples • Overflow unsigned char x = 255; x = x + 1; • Underflow unsigned char x =0; x = x - 1;

  34. Examples • Divide by zero (especially for integers!) unsigned int x = 5 / 0; • Wrong printf placeholder printf( “%s”, 57 );

  35. Errors • Four kinds of errors are relevant to CS16: • Syntax errors • Linker errors • Runtime errors • Logic errors

  36. Logic Errors • It works! • ...but it doesn’t do what you wanted • Like getting the wrong order at a restaurant

  37. Examples • Transcribed an equation incorrectly • Using the wrong variable • Lack of understanding of problem • etc. etc. etc...

  38. Logic Errors • By far, the most difficult to debug • It might be done almost correctly • This is why testing is so important!

  39. Functions • Divide and Conquer Revisited

  40. Divide and Conquer • Break a problem down into distinct subproblems, solve them individually, and finally combine them

  41. Divide and Conquer Program Input Output

  42. Divide and Conquer Subprogram 1 Input Output / Input Subprogram 2 Output

  43. Divide and Conquer Subprogram A Input Subprogram B Output / Input Subprogram 2 Output

  44. Input and Output • Intentionally left ambiguous • This general model is widely applicable Output Input Program

  45. Relation to Functions • Consider the function printf Formatting string, variables to print Something on the terminal printf

  46. printf Function printf( “%i\n”, myInteger ) “%i\n” <<myInteger on the terminal>> printf myInteger

  47. Functions • A way of breaking down programs into (more or less) independent units • Can be tested individually • Easier to think about

  48. Function Input / Output • Input: parameters (more on this later) • Output: return value

  49. Functions Function A Input Function B Output / Input Function C Output

  50. Functions Function A Input Function B Output / Input Function C Output functionC( functionA( Input ), functionB( Input ) )

More Related