Standard Functions and Scope 9 12 2007 1 Opening Discussion Let s look at interclass problem solutions Why do we use functions in C How do we add functions to a C program I have set up the online grading for this semester Your username is normal and your starting password is your 7 digit TU ID number 2 Standard Functions C provides you with a large number of standard functions So far the only parts of the standard C libraries we have seen are printf and scanf Other library calls require other header files Do man on a header name to see contents math h sin cos sqrt pow etc stdlib h abs rand etc 3 Linking the Math Library If you use the math library you will need to provide an extra flag on the command line when you compile Use lm short for link math to tell the compiler to link in the math libraries 4 Scope The term scope refer to the range of a program over which a particular name can be used C basically has two types of scope global and local Global scope means that it can be used anywhere in the program Local scope means that it can only be used in the block in which it is declared All functions are in the global scope I throw people in the fountain for putting variables in the global scope Parameters are locally scoped to the function they are passed to 5 Keys to Good Functions Large problems can be decomposed in many different ways There are several simple rules of thumb you should try to follow to come up with a good decomposition Make it meaningful Remember that we decompose problems to help us deal with complexity and make the solution easier to understand This helps if the pieces have some meaning their their names should reflect that meaning Make them flexible and reusable We will spend a lot of time trying to not duplicate code Duplicated code is bad on many levels 6 Writing more Functions We are on the verge of being able to write some real programs Let s start that today by writing a program that involves several functions 7 Minute Essay What questions do you have about functions Quiz 2 is next class Interclass Problem Do problem 34 on page 225 8
View Full Document