Class Notes (808,238)
Canada (493,099)
CS 137 (16)
Lecture 5

Programming Lecture 5.docx

3 Pages
Unlock Document

University of Waterloo
Computer Science
CS 137
Andrew Morton

Programming Lecture 5 Assertions  Syntax o assert (expression);  If expression is true, does nothing  If false, terminates program with error message  Contains filename, line number, function name, expression o Uses  Debugging  Tests assumptions and fails “loudly” rather than “quietly”  Catches future modifications that break your logic  #include bool leap (int year { assert (year>1582); if (year%400==0) return true; … }  … else if (leap(year)) printf(“%d is a leap year\n”, year); else printf(“%d is not a leap year\n”, year); printf(“%d is%s a leap year\n”, year, leap(year) ? ””:”not”); Separate Compilation  in powers.c file o int square (int num) { return num*num; } o int cube (int num) { return square(num)*num; } o int quartic (int num) { return square(square(num)); } o int quintic (int num) { return cube(num)*square(num); }  Declaration before use requires that o Square precedes cube, quartic and quintic o Cube precedes quantic  For testing, define main() and others in main.c file o # include int quartic (int number); int quantic (int number); void testQuartic(int num) { printf(“%d^4=%d\n”, nm, quartic(num)); } void testQuintic (int num) { printf(“%d^5=%d\n”, num, quantic(num)); } int main (void){ testQuartic (3); testQuntic (2); }  Function declaration o External view o Return type o Name o Parameter type(s)  Names optional o Eg:  int square (int number);  Function definition o Internal view o Includes the declaration o There can only be one per function across all files o Eg:  int square (int num
More Less

Related notes for CS 137

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.