{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## In this lesson" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [Introduction](#Introduction)\n", "- [Creating a simple single expression function](#Creating-a-simple-single-expression-function)\n", "- [Multiple expression functions](#Multiple-expression-functions)\n", "- [Flow control in a function](#Flow-control-in-a-function)\n", "- [Using optional arguments](#Using-optional-arguments)\n", "- [Using keyword arguments to bypass the order problem](#Using-keyword-arguments-to-bypass-the-order-problem)\n", "- [Functions with a variable number of arguments](#Functions-with-a-variable-number-of-arguments)\n", "- [Passing arrays as function arguments](#Passing-arrays-as-function-arguments)\n", "- [Type parameters](#Type-parameters)\n", "- [Stabby functions and do blocks](#Stabby-functions-and-do-blocks)\n", "- [Using functions as arguments](#Using-functions-as-arguments)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Introduction" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Julia is a functional language. Given specific information (called arguments), a function is a keyword that executes a task according to rules designed specifically for that function. Think of arithmetical addition as a task (a function) and the values to be added as the arguments.\n", "The term _multiple dispatch_ refers to calling the right implementation of a function based on the arguments. Note that only the positional arguments are used to look up the correct method. When the function is used again, but with different argument types, a new method is selected. This is called _overloading_." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "While we would usually think of the task of addition as a single task, adding numbers, it can in fact be seen as more than one function. One with rules for adding integers, one for adding real numbers, one for adding complex numbers, and so on. So, when we call a function (typing the specific keyword and adding the arguments is referred to as _calling the function_), we actually call a whole buch of them. Julia decides which one it is going to use based on the argument types (there is a lookup table for every function, which is stored with the function). Julia generates low-level code based on your computer's instruction set. So, when you create a function () such as...\n", "```\n", "function cbd(a)\n", " return a^3\n", "end\n", "```\n", "... a whole bunch of methods are created (the different implementations of a function are called _methods_). When the function is called with an integer argument, Julia will generate code that uses the CPU's integer multiplication instruction set and when a floating point value is used, the floating point multiplication instruction set will be targeted." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's have a look at a quintessential Julia function. You might not recognize it at first, but typing `2 + 3` is actually converted to a keyword with arguments when executed." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Adding 2 and 3\n", "2 + 3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `+` symbol is actual a function name. The typical _architecture_ of a Julia function is then a keyword, with a set of arguments, seperated by commas, all inside of a set of parenthesis." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Addition as a function\n", "+(2, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Back to the top](#In-this-lesson)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Creating a simple single expression function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Functions in Julia can be created much like a mathematical function. Below we create a function called `f` that takes a single argument. We use the character `x` as placeholder argument. The right-hand side of the equation stipulates the task that we want the function to perform, given a value for the argument." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "f (generic function with 1 method)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# A function to square the argument value\n", "f(x) = x^2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "1 method for generic function f: