{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module 2\n", "\n", "## Video 9: Defining Functions\n", "**Python for the Energy Industry**\n", "\n", "A function is a chunk of code that is defined to 'do something' to an input. The following function is defined to print the square of a number:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "def square(x):\n", " x_squared = x * x\n", " print('the square of',x,'is',x_squared)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We 'call' this function using its name, followed by round brackets containing the value to be squared:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "the square of 3 is 9\n", "the square of 5 is 25\n", "the square of 276 is 76176\n" ] } ], "source": [ "square(3)\n", "square(5)\n", "square(276)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The variable x is an 'argument' to the function square. Arguments take on the value given when the function is used. Functions can have multiple arguments:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11\n" ] } ], "source": [ "def maximum(a,b,c):\n", " if a >= b and a >= c:\n", " print(a)\n", " elif b >= a and b >= c:\n", " print(b)\n", " else:\n", " print(c)\n", " \n", "maximum(7,11,3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Default Values\n", "\n", "Function arguments can be given a 'default value' that they will take if no other value is specified. " ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def divide(dividend, divisor=3):\n", " quotient = dividend / divisor\n", " print(dividend, '/', divisor, '=', quotient)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we call `divide` with two arguments, these arguments are set to be the divident and divisor:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12 / 4 = 3.0\n" ] } ], "source": [ "divide(12,4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we only call `divide` with one argument, that is set as the dividend and the divisor takes the default value of 3." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15 / 3 = 5.0\n" ] } ], "source": [ "divide(15)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Return values\n", "\n", "`return` can be used in a function to make the function 'give back' a value. This means that you can set a variable equal to the output of a function that returns something, as follows:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The return of the function: 16\n" ] } ], "source": [ "def square(x):\n", " x_squared = x * x\n", " return(x_squared)\n", "\n", "four_squared = square(4)\n", "\n", "print('The return of the function:',four_squared)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also return multiple values from a function:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16 64 256\n" ] } ], "source": [ "def powers(x):\n", " x_squared = x**2\n", " x_cubed = x**3\n", " x_four = x**4\n", " return(x_squared,x_cubed,x_four)\n", "\n", "x2, x3, x4 = powers(4)\n", "print(x2, x3, x4)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise\n", "\n", "Define a function that takes two arguments: a list, and a value. The function should check if the value is present in the list. If the value is present, it should return the index where it is found. Otherwise, it should return nothing." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" } }, "nbformat": 4, "nbformat_minor": 4 }