Skip to main content
Mathematics LibreTexts

4.4: Variable Number of arguments

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

    From the last exercise, it would be unfortunate if we have to write different functions for different number of arguments. We can write a variable number of arguments with a ... trailing the last argument. The following is a generalized version of the mean: 

    function theMean(x::Number...) 
      local sum=0
      for val in x
        sum += val 
      end
      sum/length(x)
    end
     

    which uses a for loop that we will discuss later. This function will now find the mean using any number of arguments.

    theMean(1,2,3,4)
     

    Also, note that the argument x with the ... is a tuple, (see section 3.9). An alternative way to access the individual elements of x would be to use brackets. For example, x[3] would be the third argument.

    Exercise

    Try the following (replace the code in the above code block) and notice the resulting type:

    • theMean(11//2,5//6,1//9)
    • theMean(1.0,2.0,3.0,4.0,5.0)

     You should see that each of these return a floating point number. This is because division / with any other number returns a floating point. 

    Multiple Return Values

    A very nice feature of Julia functions is that of multiple return values. Instead of only being able to return a single number (or requiring to send an array or structural type), you can return more than 1 number (or other data type). For example.

    h(x,y) = x+y,x-y
     

    and we call this in the same manner.  For example,

    h(3,5)
     

    We can store the results of any function that returns multiple arguments in the following example:

    p,q = h(3,5)
     

    the p will take on the value 8 and q will take on the value of -2.

    The result of this function is a tuple as we saw in section XXXX. Although in that section we used parentheses around the tuple, it is not necessary and generally isn’t used to return a tuple in a function. We will use this for the result of the quadratic formula in Chapter XXXX.


    4.4: Variable Number of arguments is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?