Skip to main content
Mathematics LibreTexts

4.1: Simple Examples of Functions

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

    A simple example of a julia function is

    function sq(x)
      x^2
    end
     

    which just returns the square of the argument, called x. The name of the function is sq and the x inside the parentheses is called the argument of the function. Both the function name and any argument must adhere to the rules of variables from Chapter 3.

    An alternative way to do this is the following:

    sq(x)=x^2
     

    Calling a Function

    To use a function or often denoted calling a function is to use the function with known values for the arguments.  For example:

    sq(3)
     

    Exercise

    Find the value of the square function for values, 4, -1, 10, 2.5

    # insert your code here
     

     


    This page titled 4.1: Simple Examples 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?