# 6. Functions - 6 FUNCTIONS Rocky K C Chang October 4...

• Notes
• 33
• 100% (1) 1 out of 1 people found this document helpful

This preview shows pages 1–8. Sign up to view the full content.

6. FUNCTIONS Rocky K. C. Chang October 4, 2015 (Adapted from John Zelle’s slides)

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

Objectives To understand why programmers divide programs up into sets of cooperating functions. To be able to define new functions in Python. To understand the details of function calls and parameter passing in Python. To write programs that use functions to reduce code duplication and increase program modularity.
Functions everywhere So far, we ve seen three different types of functions: Our programs comprise a single function called main() . Built-in Python functions (e.g., print() ) Functions from the standard libraries ( math.sqrt() ) Why functions? Code reuse Easier to maintain (modularity) Facilitate solving a complex problem

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

Function definition The part of the program that creates a function is called a function definition . def <name>(<formal-parameters>): <body> E.g., def avg(n1, n2, n3): print((n1+n2+n3)/3) where n1, n2, n3 are formal parameters. A function is called/invoked by <name>(<actual- parameters>) E.g., avg(1, 2, 3) , where 1, 2 and 3 are actual parameters . Functions must be defined/imported before it is called.
EXERCISE 6.1 Try def avg(n1,n2,n3): print((n1+n2+n3)/3) avg(1, 2, 3) n1,n2,n3 = 10,20,10 avg(n1, n2, n3) What are the values of n1, n2, and n3?

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

Scope of formal parameters and variables Scope : places in a program where the parameters and variables are referenced. Formal parameters and all variables used in the function, are only accessible in the body of the function (i.e., they are local .) Variables with identical names elsewhere in the program are distinct from the formal parameters and variables inside of the function body.
EXERCISE 6.2

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

This is the end of the preview. Sign up to access the rest of the document.
• Fall '16
• Rocky Chiang
• Subroutine, def, def avg

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern