Function Basics
Function Basics
By Juan C. Giraldo
Department of Electronics
School of Engineering
Pontificia Universidad Javeriana
February 2018
A basic function in C language
declaration-listopt;
return-typeopt
Function( parameter-listopt )
{
declaration-listopt;
statement-listopt;
} /* Function */
Format of a Function in C
return-typeopt
Function( parameter-listopt )
{
declaration-listopt;
statement-listopt;
returnopt;
} /* Function */
Format of a Function in C
return-typeopt
Function( parameter-listopt )
{
declaration-listopt;
statement-listopt;
returnopt;
} /* Function */
The BUMBEST function
A tiny function in C
The SMALLEST function of them all
_(){}
Blocks in C Language
Bloque (in Spanish)
Un bloque permite agrupar a un conjunto de
declaraciones y proposiciones en una unidad
sintáctica.
[ISO/IEC 9899, pag. 131, par. 3]
{
declaration-listopt;
statement-listopt;
} /* block */
Every block has this kind of structure
{
declaration-listopt;
statement-listopt;
} /* block */
Every block has this kind of structure
} /* for */
Every block has this kind of structure
switch( expression )
{
declaration-listopt;
statement-listopt;
} /* switch */
Every block has this kind of structure
while( expression )
{
declaration-listopt;
statement-listopt;
} /* while */
Every block has this kind of structure
if( expression )
{
declaration-listopt;
statement-listopt;
} /* if */
Every block has this kind of structure
if( expression )
{ ... }
else
{
declaration-listopt;
statement-listopt;
} /* if-else */
Every block has this kind of structure
while( expression ) {
declaration-listopt;
statement-listopt;
if( expression ) {
declaration-listopt;
statement-listopt;
} /* if */
} /* while */
Every block has this kind of structure
while( expression ) {
declaration-listopt;
statement-listopt;
if( expression ) {
declaration-listopt;
statement-listopt;
} /* if */
} /* while */
… even when it is used in a FUNCTION:
A_Function()
{
declaration-listopt;
statement-listopt;
} /* A_Function */
How to use “return”
Sin un return explícito,
SIEMPRE con la llave de cierre se retorna
int
Dummy()
{
return;
} /* Simple */
“Return statements are optional and they have
the following formats [A&A, 1989, page 5]”:
return;
return exp;
return( exp );
Format of a Function in C
return-type
return( expression );
} /* Function */
¿Qué tipos de datos retorna una función?
return-type
Function()
{
...
return( expression );
} /* Function */
Always_TRUE()
{
return 1;
} /* Always_TRUE */
Function Follower returns what receives
double
Follower( double entrada )
{
return entrada;
} /* Follower */
Function Twos_Complement computes 2’
int
Twos_Complement( int number )
{
number = ~(number);
return number += 1;
} /* Twos_Complement */
Function Twos_Complement computes 2’
void
Twos_Complement(
int *number )
{
*number = ~(*number);
*number += 1;
} /* Twos_Complement */
Function Maximum returns maximum
int
Maximum( int x, int y )
{
return( (x > y) ? x : y );
} /* Maximum */
} /* main */
Function Minimum returns minimum
int
Minimum( int x, int y )
{
return( (x < y) ? x : y );
} /* Manimum */
} /* main */
Function Is_Greater queries for greater
int
Is_Greater( int a, int b )
{
if( a > b )
return 1;
else
return 0;
} /* Is_Greater */
Function Square computes squared
double
Square( double x )
{
return( x * x );
} /* Square */
Function Add does what the name indicates...
} /* Add */
Function Multiply does what the name says...
int
Multiply( int x, int y )
{
return x * y;
} /* Multiply */
Function Divide does what the name says...
double
Divide( double x, double y )
{
return x / y;
} /* Divide */
Function Return_NOTHING hace eso…
return 1;
} /* Return_TRUE */
Function Anticipated quits before reach end…
return a + b;
} /* Anticipated */
Function Not negates what receives…
int
Not( int input )
{
return !input;
} /* Not */
Function And using operator &&
int
And(
int input_1, int input_2 )
{
return input_1 && input_2;
} /* And */
Function And using operator &&
} /* And */
Function And using operator &&
return output;
} /* And */
Function And WITHOUT using operator &&
return out[in1|in2];
} /* And */
Function And WITHOUT using operator &&
return out[index];
} /* And */
Function Or using operator ||
} /* Or */
Function Xor … ¿What does this do?
} /* Xor */
THE END