Skip to main content
Engineering LibreTexts

3.1: Function calls

  • Page ID
    40864
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    We have already seen one example of a function call:

    >>> type(42)
    <class 'int'>
    

    The name of the function is type. The expression in parentheses is called the argument of the function. The result, for this function, is the type of the argument.

    It is common to say that a function “takes” an argument and “returns” a result. The result is also called the return value.

    Python provides functions that convert values from one type to another. The int function takes any value and converts it to an integer, if it can, or complains otherwise:

    >>> int('32')
    32
    >>> int('Hello')
    ValueError: invalid literal for int(): Hello
    

    int can convert floating-point values to integers, but it doesn’t round off; it chops off the fraction part:

    >>> int(3.99999)
    3
    >>> int(-2.3)
    -2
    

    float converts integers and strings to floating-point numbers:

    >>> float(32)
    32.0
    >>> float('3.14159')
    3.14159
    

    Finally, str converts its argument to a string:

    >>> str(32)
    '32'
    >>> str(3.14159)
    '3.14159'
    

    This page titled 3.1: Function calls is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?