python05.html - CHAPTER 5 Simple Functions A function is a...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 5 Simple Functions A function is a block of named, reusable, parametrized, code that performs some action. there are python built-in functions we'll learn to define our own functions by the end of this chapter we will have mentioned: print() input() format() float() int() len() str() type() keyword.iskeyword() abs() max() min() pow() round() a function has: a name the parameters it needs (if any) the return value (if any) Example: float("23") name: float argument: the string "23" (float takes sero, or, arguments only. It must be a value that can be converted to a float, anything else will cause an error). float() returns 0.0 return value: the float 23.0 vocabulary a function is "called" or "executed" a parameter is "passed" to a function a function name: consist of lower and upper case letters, digits (cannot start with a digit) underscores
Image of page 1

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

View Full Document Right Arrow Icon
cannot be a keyword import keyword keyword.iskeyword(s) keyword.kwlist parameters are the inut for the function it can be 0, a definite number, or an undefined number examples: print() # zero arguments float(23) # one argument pow(2,3) # two arguments print("hello","world","how","are","you","?") # six arguments #is there any way to write function foo so the two prints print #different values? x = 23 print(x) #1 foo(x) print(x) #2 #it really depends on the type of x, for they types we have seen the #answer is no "pass by value" means the function gets a copy of the value, so it cannot really change the variable. #all types we have seen are "passed by value" int, float, str the order of the parameters matters: >>> pow(2,3) == pow(3,2) False #some functions "return" a value: >>> x = int("20") x = int("20") #variable x is assigned the value returned by function int >>> print(x + 1) print(x + 1) 21 some functions do not return a value None is no string at all, no integer, no float, nothing.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ 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