- Call by value, not reference.
- "C"s subprograms are functions.
- Functions may be recursive.
- Functions may not be defined
within other functions.
- Functions have the form:
return-type function-name (
argument declarations )
{
declarations and statements
}
- If the return type is not specified,
it will return an int. If you don't want
this, set the return-type to void.
- A function should have a prototype before
the function is called.
#include
the necessary header (.h
) files.
- Use the ANSI style of function definition.
DO NOT use the original "C" language style..
(Well, at least stick to one of them.)
A demonstration program with a prototype for
rain
and rain
and
functions main
and spain
.
/* Program to demonstrate different function
* prototypes and declarations
*
* Louis Taber Feb 11, 2001 PCC
*/
/* library prototypes and macros */
#include <stdio.h>
double rain(double x, int y);
void cain(int r, int s);
int /* You may want the function name in Col #1 */
main(int argc, char * argv[ ], char * env[ ])
{
return 0;
}
/* You can put them on seperate lines */
/* old style */
int spain(argc, argv, env)
int argc;
char * argv[ ];
char * env[ ];
{
return 0;
}
.
I not quite sure why you would want it but
it is available by anonymous ftp at:
ftp://lt.tucson.az.us/pub/c/prototypes.c.
main
function revisited.
The following program prints out the
passed arguments and the environment
variables. According to
ANSI "C" Standard 9899 5.1.2.2.1 p 12 the only arguments
passed to main are int argc
and char *argv[ ]
.
Passing the environment is a UNIX extension.
/* Program to print arguments to a program */
/* and the passed environment */
/* L Taber March 28, 1993 PCC */
#include <stdio.h>
main(int argc, char * argv[ ], char * env[ ])
{
int i;
/* Print number of arguments and the first argument */
printf("argc = %d File name = %s\n", argc, argv[0]);
/* Print all remaining arguments - if any */
for ( i=1; i<argc; i++)
printf(" arg[%d] = %s \n" ,i,argv[i]);
printf("\n");
/* Print the environment - if any */
for ( i=0; env[i] != NULL; i++)
printf(" env[%d] = %s\n" ,i,env[i]);
printf("\n");
return 0;
}