Skip to main content
Chemistry LibreTexts

5: User-Defined Functions

  • Page ID
    419689
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    In Python you can make your own functions with the "def" statement.

    Note, you must end it with a colon, and all of the lines in the function statement must be indented, as in the following generic code.

    #Syntax for a Function
    def function_name():
        action1
        action2....
    

    Density Function

    The following code creates a function called "density_function"

    Codebox1:

    def density_function():
      mass = float(input("Enter mass in g"))
      volume = float(input("Enter volume in mL"))
      density=mass/volume
      print(f"{mass}g and {volume}mL give a density of {density}g/mL")
     

     

    Exercise \(\PageIndex{1}\)

    Click the run command for the density_function (Codebox 1).  You notice nothing happens.  Why? Is there an error?

    Answer

    All you did was create the function.  There is no error, you never called the function.

     

    Now call the density function by clicking run in codebox 2.

    Codebox 2:

    density_function() #this command calls the function
    Exercise \(\PageIndex{2}\)

    Now refresh the web page so it reloads, and call the density function without running it (run Codebox 2 without running Codebox 1).   You get an error statement.  Why?.

    Answer

    You have a name space error because you have not created the density function.

    NameError                                 Traceback (most recent call last)
    <ipython-input-1-22a51af5250c> in <module>
    ----> 1 density_function() #this command calls the function
    
    NameError: name 'density_function' is not defined

    Now run Codebox 3 without running Codebox 1:

    Codebox 3:

    def density_function():
      mass = float(input("Enter mass in g"))
      volume = float(input("Enter volume in mL"))
      density=mass/volume
      print(f"{mass}g and {volume}mL give a density of {density}g/mL")
    density_function()
            hello world
          
    Exercise \(\PageIndex{3}\)

    Now after running codebox 3 go ahead and run codebox 2 (without running codebox 1).  Why does it work?

    Answer

    Because the last line called the function

     


    This page titled 5: User-Defined Functions is shared under a not declared license and was authored, remixed, and/or curated by Robert Belford.

    • Was this article helpful?