| 1 |
|
| 2 |
<HTML> |
| 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 |
<HEAD> |
| 29 |
<TITLE>6.4 The Case of the Small Function</TITLE> |
| 30 |
<STYLE TYPE="text/css"> <!-- A {text-decoration: none} // --> </STYLE> |
| 31 |
</HEAD> |
| 32 |
<BODY BACKGROUND="binary/background.gif" |
| 33 |
BGCOLOR="#FFFFFF" |
| 34 |
TEXT="#000000" |
| 35 |
VLINK="#660000" |
| 36 |
LINK="#FF0000" |
| 37 |
ALINK="#CC0000"> |
| 38 |
|
| 39 |
<TABLE WIDTH="490"> |
| 40 |
<TR> |
| 41 |
<TD WIDTH="130" VALIGN="top"> |
| 42 |
<IMG SRC="binary/d_clear.gif" ALT="" WIDTH="130" HEIGHT="1"><BR> |
| 43 |
|
| 44 |
<FONT SIZE="2"> |
| 45 |
<BR> |
| 46 |
|
| 47 |
<A HREF="http://www.ross.net/" |
| 48 |
TARGET="rosshome" |
| 49 |
onClick="window.open('','rosshome','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 50 |
> |
| 51 |
<IMG SRC="binary/rossnet_logo.gif" |
| 52 |
WIDTH="64" HEIGHT="32" |
| 53 |
BORDER="0" ALT="RossNet" |
| 54 |
HSPACE="0" VSPACE="1"></A><BR> |
| 55 |
<BR> |
| 56 |
|
| 57 |
<A HREF="../index.shtml" |
| 58 |
TARGET="funnelweb" |
| 59 |
onClick="window.open('','funnelweb','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 60 |
> |
| 61 |
<IMG SRC="binary/linklogo.gif" |
| 62 |
WIDTH="64" HEIGHT="32" |
| 63 |
BORDER="0" ALT="FunnelWeb" |
| 64 |
HSPACE="0" VSPACE="1"></A><BR> |
| 65 |
<BR> |
| 66 |
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0><TR><TD BGCOLOR="#000000"> |
| 67 |
<A HREF="../reference/index.html" |
| 68 |
TARGET="funnelwebreference" |
| 69 |
onClick="window.open('','funnelwebreference','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 70 |
><FONT COLOR="#FFFFFF"><B>Reference</B></FONT></A><BR> |
| 71 |
<BR> |
| 72 |
<A HREF="../developer/index.html" |
| 73 |
TARGET="funnelwebdeveloper" |
| 74 |
onClick="window.open('','funnelwebdeveloper','location,status,menubar,scrollbars,resizable',false).focus(); return true;" |
| 75 |
><FONT COLOR="#FFFFFF"><B>Developer</B></FONT></A><BR> |
| 76 |
<BR> |
| 77 |
<A HREF="index.html"><FONT COLOR="#FFFFFF"><B>Tutorial</B></FONT></A><BR> |
| 78 |
<A HREF="intro.html"><FONT COLOR="#FFFFFF">1 Introduction</FONT></A><BR> |
| 79 |
<A HREF="macro.html"><FONT COLOR="#FFFFFF">2 Macros</FONT></A><BR> |
| 80 |
<A HREF="type.html"><FONT COLOR="#FFFFFF">3 Typesetting</FONT></A><BR> |
| 81 |
<A HREF="example.html"><FONT COLOR="#FFFFFF">4 Example</FONT></A><BR> |
| 82 |
<A HREF="hints.html"><FONT COLOR="#FFFFFF">5 Hints</FONT></A><BR> |
| 83 |
<A HREF="examples.html"><FONT COLOR="#FFFFFF">6 Examples</FONT></A><BR> |
| 84 |
<A HREF="web.html"><FONT COLOR="#FFFFFF">7 Webmaking</FONT></A><BR> |
| 85 |
|
| 86 |
<BR> |
| 87 |
<A HREF="search.html"><FONT COLOR="#FFFFFF"><B>SEARCH</B></FONT></A><BR> |
| 88 |
</FONT> |
| 89 |
</TD></TR></TABLE> |
| 90 |
|
| 91 |
|
| 92 |
</TD> |
| 93 |
<TD WIDTH="360" VALIGN="top"> |
| 94 |
<FONT SIZE="3"> |
| 95 |
|
| 96 |
|
| 97 |
<A HREF="../reference/index.html"><IMG SRC="binary/title.gif" |
| 98 |
WIDTH="302" HEIGHT="24" |
| 99 |
BORDER="0" ALT="FunnelWeb Tutorial Manual" |
| 100 |
HSPACE="0" VSPACE="0"></A> |
| 101 |
<P><FONT SIZE="5">6.4 The Case of the Small Function</FONT><BR> |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
<P>Often, when programming, there is a need for a code |
| 106 |
abstraction facility that |
| 107 |
operates at the text level. If the statement |
| 108 |
"<TT>a:=3;</TT>" occurs often, it may be best simply to |
| 109 |
repeat it verbatim. If a sequence of one hundred statements |
| 110 |
is repeated often, it is normal to remove the code to a |
| 111 |
function and replace the occurrences by a function call. |
| 112 |
However, in between these two extremes are cases where a |
| 113 |
particular sequence of code is long enough and appears often |
| 114 |
enough to be troublesome, but which is bound so messily to |
| 115 |
its environment as to make a function call cumbersome. |
| 116 |
|
| 117 |
<P>For example, the following line of statements |
| 118 |
(referring to five variables declared <I>local</I> to a |
| 119 |
function) might appear ten times in a function: |
| 120 |
|
| 121 |
<P> |
| 122 |
<PRE> |
| 123 |
a=b*3.14159; c=d % 256; e=e+1; |
| 124 |
</PRE> |
| 125 |
|
| 126 |
<P>Now the "normal" rule of programming says that |
| 127 |
these statements should be placed in a procedure (also |
| 128 |
called a "function" in the C programming language |
| 129 |
used in this example), but here five local variables are |
| 130 |
used. Use of a procedure (function) would result in a |
| 131 |
procedure definition looking something like: |
| 132 |
|
| 133 |
<P> |
| 134 |
<PRE> |
| 135 |
void frobit(a,b,c,d,e) |
| 136 |
float *a,b; |
| 137 |
int *c,d; |
| 138 |
unsigned *e; |
| 139 |
{*a=b << 8; *c=d % 256; *e=*e+1;} |
| 140 |
</PRE> |
| 141 |
|
| 142 |
<P>and a procedure call something like |
| 143 |
|
| 144 |
<P> |
| 145 |
<PRE> |
| 146 |
frobit(&a,b,&c,d,&e); |
| 147 |
</PRE> |
| 148 |
|
| 149 |
<P>This might be workable in a language that allowed |
| 150 |
formal parameters to be specified to be bound only to |
| 151 |
particular variables. Similarly, it might be possible to |
| 152 |
avoid the parameter list in languages that support local |
| 153 |
procedures that can access non-local variables (such as |
| 154 |
Pascal). However, in our example here, in the |
| 155 |
C programming language, these options are not available, and |
| 156 |
so we must either create a function with five parameters, or |
| 157 |
use the C macro preprocessor (the |
| 158 |
best solution). FunnelWeb provides the same macro facility |
| 159 |
for languages that do not have a built-in preprocessor. |
| 160 |
|
| 161 |
<P>In particularly speed-stressed applications, the |
| 162 |
programmer may be reluctant to remove code to a procedure |
| 163 |
because of the procedure-call overhead. FunnelWeb macros can help there too. |
| 164 |
|
| 165 |
<P>In summary, there sometimes arises in programming |
| 166 |
situations where the cost of defining a procedure is higher |
| 167 |
than the benefits it will bestow. Common reasons for this |
| 168 |
are the run-time procedure overhead and the messy binding |
| 169 |
problems caused by removing |
| 170 |
target code from its target context. FunnelWeb can help in |
| 171 |
these situations by allowing the programmer to define a text |
| 172 |
macro. This avoids all the problems and provides an |
| 173 |
additional incentive for the programmer to describe the |
| 174 |
piece of code so isolated. |
| 175 |
|
| 176 |
|
| 177 |
<P> |
| 178 |
<TABLE WIDTH="100%"> |
| 179 |
<TR> |
| 180 |
<TD ALIGN="left" VALIGN="bottom"><A HREF="examples_languages.html"><IMG SRC="binary/fw_left.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Prev"></A></TD> |
| 181 |
<TD ALIGN="center" VALIGN="bottom"><A HREF="examples.html"><IMG SRC="binary/fw_up.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Up"></A></TD> |
| 182 |
<TD ALIGN="right" VALIGN="bottom"><A HREF="examples_comments.html"><IMG SRC="binary/fw_right.gif" HEIGHT="32" WIDTH="32" BORDER="0" ALT="Next"></A></TD> |
| 183 |
</TR> |
| 184 |
</TABLE> |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
<HR> |
| 189 |
<FONT SIZE="2"> |
| 190 |
<A HREF="mailto:[email protected]">Webmaster</A> |
| 191 |
<A HREF="copyright.html">Copyright © Ross N. Williams 1992,1999. All rights reserved.</A><BR> |
| 192 |
</FONT> |
| 193 |
|
| 194 |
</FONT> |
| 195 |
</TD> |
| 196 |
</TR> |
| 197 |
</TABLE> |
| 198 |
|
| 199 |
|
| 200 |
</BODY> |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
</HTML> |