1. Home
  2. Computing & Technology
  3. C / C++ / C#

C Tutorial - Lesson Six - About Functions

By , About.com Guide

4 of 6

Learn about Function Prototypes

Applications are made up of one or more source files. Because we want to group functions into libraries, we can specify functions before we fully implement them. This makes it possible for teams of developers working on a project to compile their code, with calls to external functions before those functions are written.

Compiling precedes Linking so a file can be compiled, even if it can't be linked into an application. If your source file includes a header file with prototypes of functions that you call, then you can compile your file.

Function prototypes are one line stubs. Instead of the function block with code inside curly braces {}, it ends with a semi-colon.

Example

Taking the getpercent() function in example ex6_2 on the previous page, the prototype looks like this.
double getpercent(double a, double b ) ;
Prototypes enable the compiler to check the number and types of parameters before generating code. Before prototypes were introduced in ANSI C, earlier C compilers weren't strict about checking. As a programmer, you expect the compiler to spot silly mistakes like that, not generate faulty code!

No Parameter Functions

If your function has no parameters, use the keyword void to specify this. The function InitializeData() has no parameters.
int InitializeData( void ) ;
void EndGame( char * Message) ;
Note : InitializeData() returns an int but EndGame("Message") doesn't.

On the next page Learn about variadic functions.

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C
  5. C Tutorials
  6. Learn about Function Prototypes

©2009 About.com, a part of The New York Times Company.

All rights reserved.