summaryrefslogtreecommitdiff
path: root/book/module1/functions.tex
blob: 19a8a85a38d134aef8cd1ca1fd8c35cbc0c0428b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
\section{Functions}\label{functions}

Like a traditional mathematical functions, python functions can take an
input, process it, and give an output. In python, the input variables
are referred to as \emph{arguments}. Functions are blocks of code that
is run every time it's called. This allows us to re-use code.

Functions are defined by using the def keyword. Reminder: it is
important to keep track of indentations as it signifies the end of the
function when the indentation returns back to the same level.

\subsection{Defining Functions}\label{defining-functions}

\subsubsection{Simple function}\label{simple-function}

A simple function with no input variable can be useful if you need to
re-use code multiple times without having to re-write it.

\begin{Shaded}
\begin{Highlighting}[]
    \KeywordTok{def}\NormalTok{ function\_name():}
        \BuiltInTok{print}\NormalTok{(}\StringTok{"This is from a function"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\subsubsection{Defining a function with one
input}\label{defining-a-function-with-one-input}

We can pass variables through to the function to be processed as
follows:

\begin{Shaded}
\begin{Highlighting}[]
    \KeywordTok{def}\NormalTok{ function(x):}
        \BuiltInTok{print}\NormalTok{(x }\OperatorTok{+} \StringTok{" is best"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Note input variables can be of any data type (integer, float, string,
etc.). \#\#\# Returning values from a function If we want to calculate a
value and pass it back to the script for further use, we can use the
\texttt{return} keyword. Let's define a linear function that takes two
inputs, \texttt{x} and \texttt{b}, computes the corresponding \texttt{y}
value, and returns it so it can be used elsewhere in the code.

\begin{Shaded}
\begin{Highlighting}[]
    \KeywordTok{def}\NormalTok{ function(x, b):}
\NormalTok{        y }\OperatorTok{=} \DecValTok{3}\OperatorTok{*}\NormalTok{x}\OperatorTok{+}\NormalTok{b}
        \ControlFlowTok{return}\NormalTok{ y}
\end{Highlighting}
\end{Shaded}

For multiple output variables we can add \#\# Calling functions Now that
we've covered defining functions we want to call the function in order
to execute the block inside the function. To do this, we simply re-call
the function name as follows.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{function(}\DecValTok{2}\NormalTok{,}\OperatorTok{{-}}\DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

Note that when running this code, nothing happens. This is because we
haven't told the computer what to do with the output. Hence, if we wish
to store the output then we need to use the assign operator \texttt{=}.

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{output }\OperatorTok{=}\NormalTok{ function(}\DecValTok{2}\NormalTok{,}\OperatorTok{{-}}\DecValTok{1}\NormalTok{)}

\BuiltInTok{print}\NormalTok{(output)}
\end{Highlighting}
\end{Shaded}

In case you want to return multiple output variable from a single
function we will have\ldots{}

\subsection{Summary}\label{summary}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{def}\NormalTok{ function\_name(argument1, argument2, argument3):}
\NormalTok{    output1 }\OperatorTok{=}\NormalTok{ argument1 }\OperatorTok{*}\NormalTok{ argument2 }\OperatorTok{{-}}\NormalTok{ argument3}
\NormalTok{    output2 }\OperatorTok{=}\NormalTok{ argument2 }\OperatorTok{+}\NormalTok{ argument3}
    \ControlFlowTok{return}\NormalTok{ output1, output2}

\NormalTok{[solution1, solution2] }\OperatorTok{=}\NormalTok{ function\_name(}\DecValTok{1}\NormalTok{,}\DecValTok{2}\NormalTok{,}\DecValTok{3}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{itemize}
\tightlist
\item
  \texttt{def} - defines a function. All the code that is indented
  underneath is considered inside the function block.
\item
  \texttt{function\_name} - this is used to call the function block.
\item
  \texttt{argument1} (optional) - input variable. This is data that can
  be pass to the function. It is possible to have multiple variables
  separated by a comma. As well as can be omitted if the function should
  just give you an output such as.
\item
  \texttt{return} (optional) - if you wish to return something to your
  script, the return keyword is used. The keyword can be followed by an
  output variable or a constant. For multiple output variables, separate
  them by a comma.
\end{itemize}