===================================== Subprograms, procedures and functions ===================================== In this session we will develop a software pattern for functions where we do not distinguish between leaf and non-leaf functions. We want to support the general case where a function can call other functions and can be called by other functions. For example, function `funcA` in the program illustrated by this flow chart: ---- TIKZ ---------------------------------------------------------------------- \begin{tikzpicture} \input{flowchart.tex} \SetMargin{5}{10}{0}{3} \renewcommand\BoxWidth {5} \PutStatement{0}{A} \PutCallStatement[red!50]{1}{ call funcA } \PutStatement{2}{B} \PutCallStatement[blue!50]{3}{ call funcA } \PutStatement{4}{C} \AddPath{0}{1} \AddPath{1}{2} \AddPath{2}{3} \AddPath{3}{4} \PutAnnotation{0}{\text{Entry point}} % next flow chart column \renewcommand\FlowCol{1} \PutStatement{0}{a} \PutLabel{0}{funcA} \PutCallStatement[orange!50]{1}{ call funcB } \PutStatement{2}{b} \PutStatement{3}{return} \AddPath{0}{1} \AddPath{1}{2} \AddPath{2}{3} % next flow chart column \renewcommand\FlowCol{2} \PutStatement{0}{x} \PutLabel{0}{funcB} \PutStatement{1}{return} \AddPath{0}{1} \DrawCallPointer[red!50]{0}{1}{1}{0} \DrawReturnPointer[red!50]{0}{1}{1}{3} \renewcommand{\CallPointerPadToY}{1} \DrawCallPointer[blue!50]{0}{3}{1}{0} \DrawReturnPointer[blue!50]{0}{3}{1}{3} \renewcommand{\CallPointerPadToY}{0} \DrawCallPointer[orange!50]{1}{1}{2}{0} \DrawReturnPointer[orange!50]{1}{1}{2}{1} \end{tikzpicture} -------------------------------------------------------------------------------- Make sure to understand that this program could not be realized with the function call convention from __Session 9__. Treating this more general case requires a new and different function call convention. Because of the _curse of generality_ (i.e. "more general solutions are more complex") we will approach this by seeking the special case in the general case. Therefore we categorize functions as follows and deal with the categories in that order: - _Subprograms_ are functions that do not receive arguments and do not return a value, - _procedures_ are functions that can receive arguments but do not return a value and - _functions_ are the most general case, i.e. they can receive arguments and return a value. Note that for the terms subprogram, procedure and functions a variety of definitions exist. We use and define them such that subprograms are a special case of a procedure and procedure are a special case of a function. That's because we want to start with a simple calling convention that can be generalized step by step. ---- TIKZ ---------------------------------------------------------------------- \begin{tikzpicture} \draw[white] (-4,-1) rectangle (7,5); \draw[fill=red!20] (0,0) rectangle (6,4); \node at (6, 4) [below left] {functions}; \draw[fill=green!20] (3, 2) ellipse (2.5 and 1.25); \node at (3, 2.5) [above] {procedures}; \draw[fill=blue!20] (2.5, 1.5) ellipse (1.5 and 0.5); \node at (2.5, 1.5) {subprograms}; \draw[gray] (4.45, 1) ellipse (1.5 and 0.5); \node at (4.45, 1) [gray] {leaf functions}; \end{tikzpicture} -------------------------------------------------------------------------------- :links: Session 9 -> doc:session09/page01