Skip to main content
Mathematics LibreTexts

4.3: Multiple Dispatch

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

    Multiple Dispatch

    Before starting this, make sure that you have completed the exercise above to write a 2-argument version of the mean.  We can update this will argument types as:

    theMean(x::Number,y::Number) = (x+y)/2
    theMean (generic function with 1 method)

    It would be nice to have a mean function that takes more than 2 numbers as well, so the following is a three-argument version of the mean function can be written

    theMean(x::Real,y::Real,z::Real) = (x+y+z)/3
    theMean (generic function with 2 methods)

    Depending on the number of arguments, julia will call the appropriate function. This is an example of Multiple Dispatch, in which either the number or type of arguments determine the actual function call.

    If you look above when you declared the functions, you should see that the second one entered said theMean (generic function with 2 methods), which says that there are two functions called mean.  Another helpful command is

    methods(theMean)
    # 2 methods for generic function theMean:
    • theMean(x::Real, y::Real, z::Real) in Main at In[3]:1
    • theMean(x::Number, y::Number) in Main at In[2]:1

    and this shows that 1) there are two versions of the function and 2) where they are defined.  Note: if you have the functions in a Jupyter notebook then the In[1] and In[2] will correspond to output cells.  If instead they are defined in a specific file, the name/location of the file and the line number will be shown. 

    Multiple dispatch also allows different types of arguments as well. Let’s say we want to create a function mean that take a single string as a argument like:

    theMean(str::String) = string("This should return the definition of ",str)
    theMean (generic function with 3 methods)

    and note that this says there are now 3 method (function definitions). 

    Exercise

    Call the function theMean using the following inputs:

    • 2 and 3  (Note: separate by commas)
    • 2,3,4
    • "word"
    # insert your code here
     

    Rerun inside the code block the different arguments and you should notice that the different functions are called. 


    This page titled 4.3: Multiple Dispatch is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Peter Staab.

    • Was this article helpful?