Examples of Function Declarations and Definitions


/************************************************************************
*

The following example defines the function absolute with the return type double. Because this is a noninteger return type, absolute is declared prior to the function call.

                                                                        *
************************************************************************/


/**
 ** This example shows how a function is declared and defined
 **/

#include <stdio.h>
double absolute(double);

int main(void)
{
   double f = -3.0;

   printf("absolute number = %lf\n", absolute(f));

   return (0);
}

double absolute(double number)
{
   if (number < 0.0)
      number = -number;

   return (number);
}


/************************************************************************
*

Specifying a return type of void on a function declaration indicates that the function does not return a value. The following example defines the function absolute with the return type void. Within the function main, absolute is declared with the return type void.

                                                                        *
************************************************************************/


/**
 ** This example uses a function with a void return type
 **/

#include <stdio.h>

int main(void)
{
  void absolute(float);
  float f = -8.7;

  absolute(f);

  return(0);
}

void absolute(float number)
{
  if (number < 0.0)
    number = -number;

  printf("absolute number = %f\n", number);
}

The following code fragments show several function declarations. The first declares a function f that takes two integer arguments and has a return type of void:

      void f(int, int);

The following code fragment declares a function f1 that takes an integer argument, and returns a pointer to a function that takes an integer argument and returns an integer:

      int (*f1(int))(int);

Alternatively, a typedef can be used for the complicated return type of function f1:

      typedef int pf1(int);
      pf1* f1(int);

The following code fragment declares a pointer p1 to a function that takes a pointer to a constant character and returns an integer:

      int (*p1) (const char*);

The following declaration is of an external function f2 that takes a constant integer as its first argument, can have a variable number and variable types of other arguments, and returns type int.

      int extern f2(const int ...);

Function f3 takes an int argument with a default value that is the value returned from function f2, and that has a return type of int:

      const int j = 5;
      int f3( int x = f2(j) );

Function f6 is a constant class member function of class X with no arguments, and with an int return type:

class X
{
public:
      int f6() const;
};

Function f4 takes no arguments, has return type void, and can throw class objects of types X and Y.

class X;
class Y;
//      .
//      .
//      .
void f4() throw(X,Y);

Function f5 takes no arguments, has return type void, and cannot throw an exception.

void f5() throw();