void do_stuff (int wings, float feet, char eyes);
main()
do_stuff (3, 12.0, 4);
void do_stuff (int wings, float feet, char eyes)
# include <iostream.h>
{
int arm = 2;
float foot = 1000.0;
char lookers = 2;
do_stuff (arm, foot, lookers);
}
{
cout << "There are " << wings << "wings."
<< '\n';
cout << "There are " << feet << "feet. "
<< '\n';
cout << "There are " << int(eyes) << "eyes."
<< '\n';
}
La salida de este programa será:
There are 2 wings.
There are 3 wings.
Cada llamada a la función do_stuff() debe verificar:
There are 12 feet.
There are 4 eyes.
There are 1000 feet.
There are 2 eyes.
do_stuff (12.2, 13, 12345)
. No se produce
ningún error durante la compilación, pues estamos trabajando con tipos compatibles.
La salida del programa es:
There are 12 wings.
There are 13 feet.
There are 57 eyes.
There are 2 wings.
There are 1000 feet.
There are 2 eyes.
do_stuff (12.2, 13)
. El compilador lanzará un mensaje de error:11:error: In this statement, "do_stuff(1.22e1, 13)" supplies 2 arguments, but 3 are
expected.
do_stuff (arm, &float, lookers)
. El mensaje de error es el siguiente:13:error:In this statement, "&lookers" is of type "pointer to char", and
may not be converted to "char".
int do_stuff(int wings, float feet,
char eyes);
. Entonces, al compilar:16:error:In this declaration, the type of "do_stuff" is not compatible with the
type of previous declaration of "do_stuff".
Si ahora modificamos además el tipo devuelto en la declaración de la función,
int do_stuff (int wings, float feet, char eyes) { ...}
, obtenemos:
16:warning:Non-void function "do_stuff" does not contain a return statement.
void do_stuff (int, float, char);
Esto no dará ninguna diferencia respecto al programa original. Esto demuestra que los nombres de las variables en el prototipo son tratados como comentarios por el compilador de C++.
void do_stuff (int wings, //Number of wings
float feet,
//Number of feet
char eyes,
//Number of eyes
Esto hace que la cabecera de la función sea autoexplicatoria. Sin embargo, debe recordarse que los comentarios nunca deben usarse en lugar de nombres significativos para las variables.