Skip to main content
Engineering LibreTexts

13.1: Introduction - Functions

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

    Programmer defined functions are very useful as a means of compartmentalizing and reusing code. Once a bit of code has been created that performs a certain task, it can be reused over and over. This also makes the subsequent code more readable. If these functions prove to be widely applicable, they may be placed into custom modules so that they can be used conveniently in other programs. In Python, these functions must be defined (or imported if they’re in a module) before they are called within the main program. They may or may not have arguments and they may or may not return a value (possibly more than one).

    By comparison, think of a typical function such as round(). It takes two arguments: The variable you wish to round and the number of places to round to. It returns a single value, namely the rounded version of the original argument. For example:

    x = round( y, 2 )
    

    y and 2 are the arguments to the function and it returns a result which we then assign to the variable x (that is, x gets y rounded to 2 places). Suppose you wish to create a function that produced (returned) the parallel equivalent of two resistors. The function would look something like this, using product-sum rule:

    def parallel( r1, r2 ):
         rp = r1*r2/(r1+r2)
         return rp
    

    def indicates that this is the beginning of a function definition. Also note that the body of the function is indented, just like when using conditionals and loops. The name of the function is parallel(). It takes two arguments and returns a result based on the product-sum rule. Note that the variables r1, r2 and rp are effectively place-holders or copies of the variables that are passed. It would be used like this:

    Rx = parallel( Ra, Rb )
    

    The values of Ra and Rb are copied over to r1 and r2. Similarly, the resultant rp is copied to Rx when the function completes. This is known as passing by value (there is another technique, passing by reference, which we shall not explore). It is important to note that the variables r1, r2 and rp are local to the function, that is, they are distinct from another variable that might be named r1 or r2 in another part of your program (or in another function). So any time two resistors need to be combined in parallel, this function may be called instead of performing the calculation in-line. It makes the code much more readable. While this function is very short, functions can be hundreds of lines long and contain conditionals, loops, and all manner of code. Functions must be defined before they are called, therefore they are usually found at the beginning of programs.


    This page titled 13.1: Introduction - Functions is shared under a not declared license and was authored, remixed, and/or curated by James M. Fiore.

    • Was this article helpful?