

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Ch 6 / Foil 1
l function n predefined, programmer-defined l arguments, parameters
l return value l function call, function invocation l function description n definition
l local variables, global variables, scope l call-by-value l black box principle, information hiding, procedural abstraction
Ch 6 / Foil 3
l humans can only handle a certain (relatively low) level of complexity l a program longer than 50 (20?) lines is already probably too complex l top-down design - a method to decrease complexity a programming task by by breaking it down into a few simpler relatively independent subtasks (other method names - stepwise refinement and divide and conquer ) l if a subtask is still complex - further subdivide l each simple subtask should be coded by a separate function or other separated piece of code l added benefit - separate people (or separate teams) can work on different subtasks l black box principle is important
Ch 6 / Foil 4
l problem: a large “economy” size pizza is not always the best buy: n pizza sizes are given as a diameter (in inches) n better buy has lower price per square inch n most people cannot easily estimate it and the vendors are making an easy buck l program specification: design a program that compares two pizzas and determines which is a better buy n input: the diameters and total prices of the pizzas to compare n output: cost per square inch for each pizza and which is a better buy
Ch 6 / Foil 5
l We will use top-down design. l The subtasks are:
Ch 6 / Foil 6
l A sequence of steps is called an algorithm l algorithm for unitprice:
return (price / area) l what arguments does the function need? price, diameter l what value should it return? unit price per square inch
Ch 6 / Foil 7
l start your coding by specifying in comments what your subtasks are doing l code the tasks l if the program compiles - and produces answers - it does not mean it is correct n get your calculator and check n if your program produced correct answer once it may still be incorrect - ran more extensive test l it may still be incorrect
Ch 6 / Foil 8
l there is no way to prove that the program is correct! l A test either shows an error or demonstrates that the program behaves correctly under the particular conditions of the test l with more successful tests we gain confidence but we can never be 100% sure l How to test successfully? n Important condition - input values
Ch 6 / Foil 9
l Part of top-down design strategy - each function has to be designed, coded and tested as a separate unit l How do we do it if functions use other functions - they are not independent l wrapper - a simple function that accepts input from user and passes it straight to the function tested - you can test function under various inputs n what would be a wrapper around unitprice? l stub - a simple function that always returns reasonable (not necessarily correct values) - can be used instead of a function to test the rest of program n what would be a stub for main?
Ch 6 / Foil 10
l Program errors: n syntax - caught by compiler n run time ( bugs/logical errors ) - bugs that cannot be caught by compiler l Debugging - the process of eliminating run-time errors. Usually takes 2 to 10 times more time than writing code l The most comprehensive way to debug your program is using debugger (gdb) n you can trace your execution through (over the functions) go inside the function n locate the exact place where program fails n see and change the values of variables