110 likes | 204 Views
Lecture 10: Don't Reinvent the Wheel. Exam Results. Homework #3. Due on Monday, March 2, 2009 -- 8:00pm Exclusive OR logic ( XOR) A XOR B = ((A && !B) || (!A && B)). B. A. Software Engineer one of 10 hot jobs.
E N D
Homework #3 • Due on Monday, March 2, 2009 -- 8:00pm • Exclusive OR logic (XOR) A XOR B = ((A && !B) || (!A && B)) B A Software Engineer one of 10 hot jobs http://hotjobs.yahoo.com/career-articles-wanted_you_10_jobs_where_employers_come_looking_for_you-668
Using Functions • Easy management of large program development • Programs solving real-world problems are large. • Solution: divide-and-conquer • Construct a large program from smaller pieces or modules. • Each piece is more manageable. • Modules in C are called functions. Year Operating System SLOC (Million) 1993 Windows NT 3.1 4-5 1994 Windows NT 3.5 7-8 1996 Windows NT 4.0 11-12 2000 Windows 2000 more than 29 2001 Windows XP 40 2003 Windows Server 2003 50 Operating System SLOC (Million) Red Hat Linux 6.2 17 Red Hat Linux 7.1 30 OpenSolaris 9.7 FreeBSD 8.8 Mac OS X 10.4 86 Linux kernel 2.6.0 5.2 SLOC: source lines of code
Using Functions • Avoid re-inventing the wheel • Using existing functions as building-blocks to create new programs • “Prepackaged” functions in the C Standard Library can be used. • User-defined functions can also be used.
C Standard Library • A rich collection of functions • Common mathematical calculations • String manipulations • Character manipulations • Input/output • Others • Provide many of the capabilities one need. http://en.wikipedia.org/wiki/C_standard_library
Function Calls • Invoking functions (by a function call) • Provide function name and arguments (data) • Function performs operations or manipulations • Function returns results • A common analogy of function call: • Boss asks worker to complete a task. Worker gets information, does task, returns result. Information hiding: boss does not know details. The calling function, or caller Hierarchical boss function/worker function relationship The called function, or callee
Math Library Functions • Math library functions • Perform common mathematical calculations • #include <math.h> • While compiling, use the option -lm to link the math library. • Format for calling functions • FunctionName( argument ); • If multiple arguments, use comma-separated list • Example printf(“%.2f”, sqrt( 900.0 )); • Calls function sqrt, which returns the square root of its argument. • All math functions return data type double. • Arguments may be constants, variables, or expressions
Law of Cosines Leg 1 Leg 2 Hypotenuse