Cse test 2 - -second midterm from here-8 Functions1 A function is an element from mathematics It is a mapping taking in arguments and returning a

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: -----------------------second midterm from here--------------------8 Functions1. A function is an element from mathematics. It is a mapping, taking in arguments and returning a single value2. Functions are an encapsulation of a program. They are useful because they support:a. reusable code (can be used in many places)b. encapsulated code (details of implementation are hidden)c. is portable as modules/libraries (can be imported)3. Function invocation starts the function:a. passes argumentsb. return value (if there is one) is captured by an assignment4. Function definition:a. receives parametersb. defines the operation of the function5. Argument to Parameter matching:a. first argument of the invocation is matched to the first parameter of the definition.b. each function defines a local namespace. Remember, namespaces hold pairs of variable names and the values they hold.c. the value stored in the parameter is the value of what was stored in the argument. Python does pass by values6. What do you get with pass by value?a. The entry in the namespace for that variable. That is, a reference to the value (see slide diagrams)b. if the value referred to is an immutable, then a change creates a new valuec. if the value referred to is a mutable, then a change updates that value.7. Local function variablesa. variables are created when they are assignedb. if a variable is assigned within the suite of a function, then that variable only exists within that suite. Once the suite ends, the variable disappearsc. this is because the function maintains its own namespace. If the variable is created in the function, it only exists in the functions namespace. When the function ends, the namespace ends as well.8. More on why functions?a. break a larger program down into smaller, understandable parts.b. for easier update or “refactoring”. Refactoring, as applied to functions, takes a larger piece of code and breaks it down into smaller function pieces. This makes it easier to maintain.9. How to write a functiona. should do “one” thing. It represents one “idea” to be implementedb. should not be longc. should be generic, that is it should be reusable (used in other code).d. Should be readable!!!10. Can you do all the examples!8.5 String Formatting1. Move beyond the defaults of the print function. Format strings. Do this by actually changing the form of the string.2. Know the basic form as shown in the slides, such as:a. “%s is %d years old” % (“Bill”,25) => “Bill is 25 years old”b. formatting examples are:i. %s stringii. %d decimaliii. %e floating point with exponentiv. %f floating point as decimalv. %u unsigned intc. can have widths (%10s is a string 10 chars wide, %-10s is left justified)d. can have precision in numbers, %.4f is 4 decimal places, %10.2f is 10 spaces total, 2 decimal points3. Did you look at the examples?...
View Full Document

This note was uploaded on 04/03/2008 for the course CSE 231 taught by Professor Punch during the Spring '07 term at Michigan State University.

Page1 / 2

Cse test 2 - -second midterm from here-8 Functions1 A function is an element from mathematics It is a mapping taking in arguments and returning a

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online