Skip to main content
Mathematics LibreTexts

4.2: Input and Output of Functions

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

    Function Arguments

    The function arguments are the numbers or strings or an element of any data type that is passed into the function. In the function sq above, the number x is the only argument. We can have more arguments by separating by commands. The (quite unnecessary function) theSum will take two arguments and add the result:

    theSum(x,y) = x+y
    theSum (generic function with 1 method)

    Typically, if a function can be written in one line, we will use this style of functions. For

    more complex functions, use the function keyword as a block of code. 

    Exercise

    Write a function theMean that finds the mean (average) of two arguments x and y. 

    # insert your code here
     

    Returning values from a function

    The functions sq or theSum returned the value that is the last line of the function. If you want to return a value before the last line you can use the return command. The following will return true if the number is odd and false if the number is true:

    function isOdd(n) 
      if mod(n,2)==1
        return true
      end
      false 
    end
    isOdd (generic function with 1 method)

    The mod function is the remainder of n divided by 2 and can also be written n % 2. If the remainder is 1, then the number is odd. On line #3, return true the code stops here and exits the function and doesn’t execute any of the other lines.  Note: the == tests for equality. This will be discussed in chapter XXX.

    A couple of short tests follow:

    isOdd(3)
    true
    isOdd(10)
    false

    A shorter way to write this (but doesn’t use the return statement) just evaluates if mod(n,2) is 1 or not:

    isOdd(n) = mod(n,2)==1
    isOdd (generic function with 1 method)

    which will return true if mod(n,2) is actually 1 and false if it is anything else (but the only other possibility is 0). Again, since this is just one line, we’ll use the shorthand notation.

    Note: The second isOdd function replaces the original one.  This is much like the idea of a variable value being updated. 

    Indentation in Functions

    You should notice that the isOdd function written above as a block of 5 lines has different indentation. In Julia, the spaces don’t matter but it is standard to indent for clarity. Notice first that in all of the functions so far, the code has been indented two spaces and then the if block is indented again 2 spaces.  There is nothing special about 2 spaces.  This is often a personal style and others use 3 or 4 spaces.  


    This page titled 4.2: Input and Output of Functions is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Peter Staab.

    • Was this article helpful?