[translation] functions, half way done

This commit is contained in:
Jan Grewe 2016-10-13 13:47:00 +02:00
parent b31820e43f
commit 40d2eb4aa0

View File

@ -1322,94 +1322,100 @@ works as intended. A solution for this problem are the
\subsection{Functions} \subsection{Functions}
Eine Funktion in \matlab{} wird \"ahnlich zu einer mathematischen Functions in \matlab{} are similar to a mathematical functions
Funktion definiert: \[ y = f(x) \] Here, the mathematical function has the name $f$ and it
\[ y = f(x) \] has one \codeterm{argument} $x$ that is transformed into the
Die Funktion hat einen Namen $f$, sie \"uber das Argument $x$ function's output value $y$. In \matlab{} the syntax of a function
einen Input und liefert ein Ergebnis in $y$ zur\"uck. Listing declaration is very similar (listing~\ref{functiondefinitionlisting}).
\ref{functiondefinitionlisting} zeigt wie das in \matlab{} umgesetzt
wird. \begin{lstlisting}[caption={Declaration of a function in \matlab{}}, label=functiondefinitionlisting]
\begin{lstlisting}[caption={Funktionsdefinition in \matlab{}}, label=functiondefinitionlisting]
function [y] = functionName(arg_1, arg_2) function [y] = functionName(arg_1, arg_2)
% ^ ^ ^ % ^ ^ ^
% Rueckgabewert Argument_1, Argument_2 % return value argument_1, argument_2
\end{lstlisting} \end{lstlisting}
Ein Funktion beginnt mit dem Schl\"usselwort \code{function} gefolgt The keyword \code{function} is followed by the return value(s) (it can
von den R\"uckgabewerte(n), dem Funktionsnamen und (in Klammern) den be a list \code{[]} of values), the function name and the
Argumenten. Auf den Funktionskopf folgt der auszuf\"uhrende argument(s). The function head is then followed by the function's
Programmcode im Funktionsk\"orper. Die Funktionsdefinition wird body. A function is ended by and \code{end} (this is in fact optional
% optional %XXX es ist vielleicht optional, aber gute stil ware es immer hinzuschreiben, oder? but we will stick to this). Each function that should be directly used
mit einem \code{end} abgeschlossen. Jede Funktion, die vom by the user (or called from other programs) should reside in an
Nutzer direkt verwendet werden soll, ist in einer eigenen Datei individual \code{m-file} that has the same name as the function. By
definiert. \"Uber die Definition/Benutzung von Funktionen wird folgendes erreicht: using functions instead of scripts we gain several advantages:
\begin{itemize} \begin{itemize}
\item Kapseln von Programmcode, der f\"ur sich eine Aufgabe l\"ost. \item Encapsulation of program code that solves a certain task. It can
\item Definierte Schnittstelle. be easily re-used in other programs.
\item Eigener G\"ultigkeitsbereich: \item There is a clear definition of the function's interface. What
does the function need (the arguments) and what does it return (the
return values).
\item Separated scope:
\begin{itemize} \begin{itemize}
\item Variablen im Workspace sind in der Funktion \emph{nicht} sichtbar. \item Variables that are defined within the function do not appear
\item Variablen, die in der Funktion definiert werden erscheinen in the workspace and cannot cause any harm there.
\emph{nicht} im Workspace. \item Variables that are defined in the workspace are not visible to
the function.
\end{itemize} \end{itemize}
\item Erh\"oht die Wiederverwendbarkeit von Programmcode. \item Functions increase re-usability.
\item Erh\"oht die Lesbarkeit von Programmen, da sie \item Increase the legibility of programs since they are more clearly
\"ubersichtlicher werden. arranged.
\end{itemize} \end{itemize}
Das Folgende Beispiel (Listing \ref{badsinewavelisting}) zeigt eine The following listing (\ref{badsinewavelisting}) shows a function that
Funktion, die eine Reihe von Sinusschwingungen unterschiedlicher calculates and displays a bunch of sines with different amplitudes.
Frequenzen berechnet und graphisch darstellt.
\begin{lstlisting}[caption={Ein schlechtes Beispiel einer Funktion, die eine Reihe Sinusse plottet.},label=badsinewavelisting] \begin{lstlisting}[caption={Bad example of a function that displays a series of sines.},label=badsinewavelisting]
function meineErsteFunktion() % Funktionskopf function meineFirstFunction() % function head
t = (0:0.01:2); % hier faengt der Funktionskoerper an t = (0:0.01:2);
frequenz = 1.0; frequency = 1.0;
amplituden = [0.25 0.5 0.75 1.0 1.25]; amplitudes = [0.25 0.5 0.75 1.0 1.25];
for i = 1:length(amplituden) for i = 1:length(amplitudes)
y = sin(frequenz * t * 2 * pi) * amplituden(i); y = sin(frequency * t * 2 * pi) * amplituden(i);
plot(t, y) plot(t, y)
hold on; hold on;
end end
end end
\end{lstlisting} \end{lstlisting}
Das obige Beispiel ist ein Paradebeispiel f\"ur eine schlechte
Funktion. Sie hat folgende Probleme: \code{myFirstFunction} (listing~\ref{badsinewavelisting}) is a
prime-example of a bad function. There are several issues with it's
design:
\begin{itemize} \begin{itemize}
\item Der Name ist nicht aussagekr\"aftig. \item The function's name does not tell anything about it's purpose.
\item Die Funktion ist f\"ur genau einen Zweck geeignet. \item The function is made for exactly one use-case (frequency of
\item Was sie tut, ist festgelegt und kann von au{\ss}en nicht 1\,Hz and five amplitudes).
beeinflusst oder bestimmt werden. \item The function's behavior is \enterm{hard-coded} within it's body
\item Sie tut drei Dinge auf einmal: Sinus berechnen \emph{und} and cannot be influenced without changing the function itself.
Amplituden \"andern \emph{und} graphisch darstellen. \item It solves three tasks at the same time: calculate sine
\item Es ist nicht (einfach) m\"oglich an die berechneten Daten zu \emph{and} change the amplitude \emph{and} plot the result.
kommen. \item There is no way to access the calculated data.
\item Keinerlei Dokumentation. Man muss den Code lesen und rekonstruieren, was sie tut. \item No documentation. One has to read and understand the code to
learn what is does.
\end{itemize} \end{itemize}
Bevor wir anfangen die Funktion zu verbessern mu{\ss} definiert werden Before we can try to improve the function the task should be clearly
was das zu l\"osende Problem ist: defined:
\begin{enumerate} \begin{enumerate}
\item Welches Problem soll gel\"ost werden? \item Which problem should be solved?
\item Aufteilen in Teilprobleme. \item Can the problem be subdivided into smaller tasks?
\item Gute Namen finden. \item Find good names for each task.
\item Definieren der Schnittstellen --- Was m\"ussen die beteiligten Funktionen \item Define the interface. Which information is necessary to solve
wissen? Was sollen sie zur\"uckliefern? each task and which results should be returned to the caller
\item Daten zur\"uck geben (R\"uckgabewerte definieren). (e.g. the user of another program that calls a function)?
\end{enumerate} \end{enumerate}
Das Beispielproblem aus Listing \ref{badsinewavelisting} kann in drei
Teilprobleme aufgetrennt werden. (i) Berechnen der \emph{einzelnen} As indicated above the \code{myFirstFunction} does three things at
Sinusse. (ii) Plotten der jeweils berechneten Daten und (iii) once, it seems natural, that the task should be split up into three
Koordination von Berechnung und Darstellung mit unterschiedlichen parts. (i) Calculation of the individual sine-wave defined by the
Amplituden. frequency and the amplitude (ii) graphical display of the data and
(iii) coordination of calculation and display.
\paragraph{I. Berechnung eines einzelnen Sinus}
\paragraph{I. Calculation of a single sine-wave}
Die Berechnung eines einzelnen Sinus ist ein typischer Fall f\"ur eine
Funktion. Wiederum macht man sich klar, (i) wie die Funktion Before we start coding it is best, to again think about the task and
hei{\ss}en soll, (ii) welche Information sie ben\"otigt und (iii) define (i) how to name the function, (ii) which information it needs
welche Daten sie zur\"uckliefern soll. (arguments), and (iii) what it should return to the caller.
\begin{enumerate} \begin{enumerate}
\item \codeterm[Funktion!Name]{Name}: der Name sollte beschreiben, was \item \codeterm[Funktion!Name]{Name}: der Name sollte beschreiben, was