Files
oldlinux-files/Ref-docs/c_lib_guide/1.3.html
2024-02-19 00:21:47 -05:00

159 lines
5.2 KiB
HTML

<html>
<head>
<title>
C Guide--1.3 Functions
</title>
<!-- Changed by: eric huss, 12-Mar-1997 -->
</head>
<body text="#000000" bgcolor="#FFFFFF">
<center>
<table border=0 width=100%>
<tr>
<td align=left width=20% valign=top>
<a href="1.2.html">
<img src="left.gif" border=0>
Previous Section<br>
1.2 Identifiers</a></td>
<td align=center width=60% valign=top>
| <a href="index.html">Table of Contents</a> |
<a href="index2.html">Index</a> |</td>
<td align=right width=20% valign=top>
<a href="1.4.html">
Next Section
<img src="right.gif" border=0><br>
1.4 References</a></td>
</tr>
</table>
</center>
<p>
<hr>
<h1>1.3.1 Function Definition</h1>
<p>
A function is declared in the following manner:
<blockquote>
<i>return-type function-name</i><code><b>(</b></code><i>parameter-list,...</i><code><b>) { </b></code><i>body...</i><code><b> }</b></code>
</blockquote>
<i>return-type</i> is the variable type that the function returns. This can
not be an array type or a function type. If not given, then
<code><b>int</b></code> is assumed.
<p>
<i>function-name</i> is the name of the function.
<p>
<i>parameter-list</i> is the list of parameters that the function takes
separated by commas. If no parameters are given, then the function does
not take any and should be defined with an empty set of parenthesis or
with the keyword <code><b>void</b></code>. If no variable type is in front
of a variable in
the paramater list, then <code><b>int</b></code> is assumed. Arrays and
functions are not
passed to functions, but are automatically converted to pointers. If the
list is terminated with an ellipsis (<code><b>,...</b></code>), then there
is no set number of
parameters. Note: the header <code><b>stdarg.h</b></code> can be used to
access arguments when using an ellipsis.
<p>
If the function is accessed before it is defined, then it must be
prototyped so the compiler knows about the function. Prototyping normally
occurs at the beginning of the source code, and is done in the following
manner:
<blockquote>
<i>return-type function-name</i><code><b>(</b></code><i>paramater-type-list</i><code><b>);</b></code>
</blockquote>
<i>return-type</i> and <i>function-name</i> must correspond exactly to the
actual
function definition. <i>parameter-type-list</i> is a list separated by
commas of
the types of variable parameters. The actual names of the parameters do
not have to be given here, although they may for the sake of clarity.
<p>
Examples:
<blockquote>
<code><b>int joe(float, double, int);</b></code><br>
This defines the prototype for function joe.<br>
<br>
<pre><code><b>int joe(float coin, double total, int sum)
{
/*...*/
}</b></code></pre>
This is the actual function joe.<br>
<br>
<code><b>int mary(void), *lloyd(double);</b></code><br>
This defines the prototype for the function mary with no parameters and
return type int. Function llyod is defined with a double type paramater
and returns a pointer to an int.<br>
<br>
<code><b>int (*peter)();</b></code><br>
Defines peter as a pointer to a function with no parameters specified.
The value of peter can be changed to represent different functions.<br>
<br>
<code><b>int (*aaron(char *(*)(void)) (long, int);</b></code><br>
Defines the function aaron which returns a pointer to a function. The
function aaron takes one argument: a pointer to a function which returns a
character pointer and takes no arguments. The returned function returns a
type int and has two parameters of type long and int.
</blockquote>
<h1>1.3.2 Program Startup</h1>
<p>
A program begins by calling the function main. There is no prototype
required for this. It can be defined with no parameters such as:
<blockquote>
<code><b>int main(void) { </b></code><i>body...</i><code><b> }</b></code>
</blockquote>
Or with the following two parameters:
<blockquote>
<code><b>int main(int argc, char *argv[]) { </b></code><i>body...</i><code><b> }</code></b>
</blockquote>
Note that they do not have to be called <code><b>argc</b></code> or
<code><b>argv</b></code>, but this is the common naming system.
<p>
<code><b>argc</b></code> is a nonnegative integer. If <code><b>argc</b></code> is greater than zero, then
the string pointed to by <code><b>argv[0]</b></code> is the name of the program. If <code><b>argc</b></code> is
greater than one, then the strings pointed to by <code><b>argv[1]</b></code> through
<code><b>argv[argc-1]</b></code> are the parameters passed to the program by the system.
<p>
Example:
<pre><code><b>
#include&lt;stdio.h&gt;
int main(int argc, char *argv[])
{
int loop;
if(argc>0)
printf("My program name is %s.\n",argv[0]);
if(argc>1)
{
for(loop=1;loop&lt;argc;loop++)
printf("Parameter #%i is %s.\n",loop,argv[loop]);
}
}
</b></code></pre>
<hr>
<center>
<table border=0 width=100%>
<tr>
<td align=left width=20% valign=top>
<a href="1.2.html">
<img src="left.gif" border=0>
Previous Section<br>
1.2 Identifiers</a></td>
<td align=center width=60% valign=top>
| <a href="index.html">Table of Contents</a> |
<a href="index2.html">Index</a> |</td>
<td align=right width=20% valign=top>
<a href="1.4.html">
Next Section
<img src="right.gif" border=0><br>
1.4 References</a></td>
</tr>
</table>
</center>
</body>
</html>