|
| 1 | +\taughtsession{Lecture}{Subprograms and Parameter Passing}{2024-04-26}{14:00}{Jiacheng}{} |
| 2 | + |
| 3 | +\section{Subprograms} |
| 4 | +A common technique used to manage complexity of a larger problem is to decompose the problem into a series of smaller sub-problems. These smaller problems are easier to solve; however it is necessary that the programming language supports decomposition.\\ |
| 5 | + |
| 6 | +The fundamental concept, provided by all modern programming languages is the subprogram, procedure or function.\\ |
| 7 | + |
| 8 | +A \textit{subprogram} is a piece of code that is identified by a name, is given a local reference environment of its own, and can exchange information with the rest of the code using parameters. Subprograms come in two flavours - procedures and functions.\\ |
| 9 | + |
| 10 | +Both \textit{procedures} and \textit{functions} are types of subprogram. Functions normally have a return value however procedures do not. They both accept parameters and perform code operations on those parameter values. During this lecture - we will only consider functions, however the concepts are also applicable to procedures. |
| 11 | + |
| 12 | + |
| 13 | +\section{Functions \& Abstraction} |
| 14 | +Functions provide one of the two fundamental abstraction facilities of programming languages: |
| 15 | + |
| 16 | +\begin{description} |
| 17 | + \item[Process / Control abstraction] in which programmers can hide procedural detail and only be concerned with a procedures interface, rather than its implementation |
| 18 | + \item[Data abstratction] in which sophisticated data types can be used without knowing how they are implemented. This separates concerns and promotes highly reusable code as well as making code more maintainable. This will be covered further in a different lecture |
| 19 | +\end{description} |
| 20 | + |
| 21 | +An example of a function is shown below: |
| 22 | +\begin{verbatim} |
| 23 | + int foo (int n, int a){ |
| 24 | + int temp = a; |
| 25 | + if (temp == 0){ |
| 26 | + return n; |
| 27 | + } else{ |
| 28 | + return n + 1; |
| 29 | + } |
| 30 | + } |
| 31 | + ... |
| 32 | + int x; |
| 33 | + x = foo(3, 0); |
| 34 | + x = foo(x+1, 1); |
| 35 | +\end{verbatim} |
| 36 | + |
| 37 | +The definition of the function refers to the actual implementation of the function. It describes the interface (header) and the actions of the function (body). The header compeises of the name, return type and the formal parameters of a function. The parameter profile of a function is the number, order and types of its parameters, for example: |
| 38 | +\begin{verbatim} |
| 39 | + (int n, int a) |
| 40 | +\end{verbatim} |
| 41 | + |
| 42 | +A function call is an explicit request that the function be executed. For example: |
| 43 | +\begin{verbatim} |
| 44 | + y = foo(3, 0); |
| 45 | +\end{verbatim} |
| 46 | + |
| 47 | +\subsection{Function Declarations} |
| 48 | +A function definition (known as a \textit{prototype} in C and C++) provides the header but not the body of a function. The parameter names can be omitted and it could be saved in a separate file, which contains only the declaration of the functions.\\ |
| 49 | + |
| 50 | +The function definition comes after the declaration of the function. |
| 51 | + |
| 52 | +\subsection{C \& C++ Prototypes and Header Files} |
| 53 | +Prototypes are often written in a separate header file which can be included in other C source files that wish to use the functions. For example, the header file \verb|stdio.h| contains prototypes for the standard input \& output files \verb|scanf| and \verb|printf|. THe actual source code is stored in a library elsewhere on the computer, however the header file says how to interface with the functions. |
| 54 | + |
| 55 | +\section*{Parameters \& Return Values} |
| 56 | +A parameter is a way in which data can be provided to the function. There are a number of different types: |
| 57 | +\begin{description} |
| 58 | + \item[formal parameters] are dummy variables listed in the function header and used in the function body |
| 59 | + \item[actual parameters] represent a value or address used in the function call statement |
| 60 | +\end{description} |
| 61 | + |
| 62 | +Functions use \textit{return values} to return values to the calling program at the end of the function's execution. This calling program, which called the function, is suspended during execution of the called function. Control always return to the caller when function's execution terminates. Note that the calling program could be another function. |
| 63 | + |
| 64 | +\subsection{Parameter Binding} |
| 65 | +There are two ways to bind the actual parameters to the formal ones: |
| 66 | +\begin{description} |
| 67 | + \item[By position] means that the binding of actual parameters to formal parameters is by their order of appearance, therefore the first parameter is bound to the first formal parameter and so forth. This is safe and effective |
| 68 | + \item[By Keyword] means that the names of the formal and actual parameters are used. This means that parameters can appear in any order, thereby avoiding parameter correspondence errors; however the user must remember the formal parameters names. |
| 69 | +\end{description} |
| 70 | + |
| 71 | +An example of By Position is shown below: |
| 72 | +\begin{verbatim} |
| 73 | + x = foo(3, 0); |
| 74 | +\end{verbatim} |
| 75 | +An example of By Keyword is shown below: |
| 76 | +\begin{verbatim} |
| 77 | + func(value=my_value, array=my_array) |
| 78 | + func(array=my_array, value=my_value) |
| 79 | +\end{verbatim} |
| 80 | + |
| 81 | +\subsection{Local Referencing Environment} |
| 82 | +When a function is called, a local referencing environment which includes all local variables is created. IN most contemporary languages, local variables are created on the stack and are stack-dynamic (dynamic meaning the variables are created on demand, and stack meaning there is a natural implementation of recursion).\\ |
| 83 | + |
| 84 | +Local variables, however, can be static. This is especially helpful when their values are to be shared between different calls. For example, in C and C++, locals are by default stack-dynamic however cna be declared \verb|static|. |
| 85 | + |
| 86 | +\section{Parameter Passing Methods} |
| 87 | +The \textit{parameter passing method} is the method in which parameters are transmitted to and / or from the called subprograms. THe relationship between the actual parameters is characterised by one of three semantic models: |
| 88 | +\begin{itemize} |
| 89 | + \item formal parameters receive data from the corresponding actual parameters; |
| 90 | + \item formal parameters transmit data to the actual parameters; |
| 91 | + \item do both |
| 92 | +\end{itemize} |
| 93 | + |
| 94 | +These models are called: in mode, out mode and inout mode, respectively. Further information on these models is below: |
| 95 | +\begin{description} |
| 96 | + \item[In Mode] is implemented as pass by value. This is where the values are copied to the formal parameters |
| 97 | + \item[Out Mode] is implemented as pass by result. This is where the values are copied to the actual parameters |
| 98 | + \item[Inout Mode] is implemented as pass by value and result, which is a combination of pass by value (on function entry at the moment when functions are called) and pass by result (on termination of functions); or implemented as pass by reference where an access path (for example, an address) is transmitted to a formal parameter. |
| 99 | +\end{description} |
| 100 | + |
| 101 | +\subsection{Pass by Value} |
| 102 | +Pass by Value works by transmitting the value when the function is called, and the value of the actual parameter is used to initialise the corresponding formal parameter. For example: |
| 103 | +\begin{verbatim} |
| 104 | + void foo (int x){ |
| 105 | + x = x + 1; |
| 106 | + } |
| 107 | + int y = 1; |
| 108 | + foo(y + 1); |
| 109 | +\end{verbatim} |
| 110 | +The above code works by, during the execution of \verb|foo|, local variable \verb|x| assumes the initial value \verb|2| by the effect of passing the parameter. It is then increased to \verb|3| (by the operations of the function body) and is finally destroyed on termination of the function call (execution control returns to the caller), therefore the value of \verb|3| is lost. \\ |
| 111 | + |
| 112 | +Through this method, we take a copy of the values of the parameters which separates the actual parameters from the formal parameters which could be modified in the function; meaning that the original values (in the calling program) are not altered during the function execution. It is simple and fast for transmitting a few scalar (elementary types) values. Pass by Value is the default mechanism in many languages, for example C, C++, Java and pascal.\\ |
| 113 | + |
| 114 | +However, additional storage is required (as the parameter values end up being stored twice) and the actual move of data can be costly (for parameters which contain a large amount of data). |
| 115 | + |
| 116 | +\subsection{Pass by Result} |
| 117 | +Pass by Result works by transmitting the value of the parameters back to the calling program when execution terminates. Note that no value is transmitted from the caller to the function.\\ |
| 118 | + |
| 119 | +During the execution of the function, the formal parameters acts as local variables of the function and their values are copied to the real parameters when control is returned to the caller. An example of this is shown below: |
| 120 | +\begin{verbatim} |
| 121 | + void foo (result int x){ |
| 122 | + x=8 |
| 123 | + } |
| 124 | + ... |
| 125 | + int y = 1; |
| 126 | + foo(y); |
| 127 | +\end{verbatim} |
| 128 | +After the function call, the value of \verb|y| becomes \verb|8|. When calling such functions in proper code, you shouldn't use actual values as the parameters to call them.\\ |
| 129 | + |
| 130 | +A major limitation to this solution is that the actual parameters must be variables. However, there is a potential problem to this solution. In that the following C\# method specifies the pass by result by putting \verb|out| specifiers on it's formal and actual parameters. At the end of execution of the method, if \verb|x| is copied to \verb|a| firs, then the value of \verb|a| will be \verb|30|. If \verb|y| is copied first, then \verb|a| will be \verb|15|. This shows that the value of \verb|a| depends on the order of copying, which varies from language to language and implementation to implementation. |
| 131 | + |
| 132 | +\begin{verbatim} |
| 133 | + void myMethod (out int x, out int y){ |
| 134 | + x = 15; |
| 135 | + y = 30; |
| 136 | + } |
| 137 | + ... |
| 138 | + int a = 0; |
| 139 | + someObj.myMethod(out a, out a); |
| 140 | +\end{verbatim} |
| 141 | + |
| 142 | +\subsection{Pass by Value-Result} |
| 143 | +Unsurprisingly, as the name would suggest, Pass by Value-Result is a combination of pass by value and pass by result. It is sometimes known as pass-by-copy, and an example of it can be seen below: |
| 144 | +\begin{verbatim} |
| 145 | + void foo (valueresult int x) { |
| 146 | + x = x + 1; |
| 147 | + } |
| 148 | + ... |
| 149 | + int y = 8; |
| 150 | + foo(y); |
| 151 | +\end{verbatim} |
| 152 | + |
| 153 | +At the point of call, the value of actual parameters are copied to and stored in the formal parameters; and at the end of the call, the values of formal parameters are copied to the actual parameters.\\ |
| 154 | + |
| 155 | +The pass by value-result method facilities bi-directional data exchange, however isolates the formal parameters from the actual parameters. |
| 156 | + |
| 157 | +\subsection{Mixture Mode of Parameter Passing} |
| 158 | +In a function, multiple different parameter passing mechanisms may be used depending upon languages and implementations. For example, in Ada: |
| 159 | +\begin{verbatim} |
| 160 | + Procedure ADDER (A: in out INTEGER; |
| 161 | + B: in INTEGER; |
| 162 | + C: out FLOAT;) |
| 163 | +\end{verbatim} |
| 164 | + |
| 165 | +\subsection{Pass by Reference} |
| 166 | +In Pass by Reference, instead of passing values, access paths are passed. This is also called pass-by-sharing (referring to sharing a memory address between the calling program and the function). In this mode, the formal parameter and it's corresponding actual parameters are both referring to the same variable (and therefore the same memory address), even if they use different names.\\ |
| 167 | + |
| 168 | +This allows for bi-directional data exchange, whereby each modification of the formal parameter is a modification of the actual parameter. This can be seen in the following example: |
| 169 | +\begin{verbatim} |
| 170 | + void foo (reference int x){ |
| 171 | + x = x + 1 |
| 172 | + } |
| 173 | + ... |
| 174 | + int y = 0; |
| 175 | + foo(y); |
| 176 | +\end{verbatim} |
| 177 | + |
| 178 | +During the execution of \verb|foo|, \verb|x| is a name for \verb|y|. After the call \verb|y| becomes \verb|1|. \\ |
| 179 | + |
| 180 | +An advantage of this methodology is that the passing process is efficient, in that there is no copying and no duplicated storage. However, there is no separation between formal and actual parameter; and the accessed formal parameters might be slower (when compared to pass-by-value, where local variables are used).\\ |
| 181 | + |
| 182 | +Pass-by-reference is a low-level operation (for example only an address needs to be stored). It has been excluded from some modern languages - for example Java, C and C++. In these languages, some forms of bidirectional communication between the caller and callee can still be achieved. |
0 commit comments