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

Info icon 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)
Image of page 1

Info icon 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.
Image of page 2
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
Image of page 3

Info icon 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.
Image of page 4
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?
Image of page 5

Info icon 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.
Image of page 6
EXERCISE 6.2
Image of page 7

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

Image of page 8
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

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern