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

# Programming Lecture 5.docx

3 Pages
141 Views

School
University of Waterloo
Department
Computer Science
Course
CS 137
Professor
Andrew Morton
Semester
Fall

Description
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

OR

Don't have an account?

Join OneClass

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

Join to view

OR

By registering, I agree to the Terms and Privacy Policies
Just a few more details

So we can recommend you notes for your school.