diff options
Diffstat (limited to 'book/module1/functions.tex')
| -rw-r--r-- | book/module1/functions.tex | 110 |
1 files changed, 0 insertions, 110 deletions
diff --git a/book/module1/functions.tex b/book/module1/functions.tex deleted file mode 100644 index 19a8a85..0000000 --- a/book/module1/functions.tex +++ /dev/null @@ -1,110 +0,0 @@ -\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} |
