You are on page 1of 46

Functions in C

Design of Embedded Computer Systems

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( expopt; expopt; expopt )


{
declaration-listopt;
statement-listopt;

} /* 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

Ambos son del mismo tipo


Function()
{
...

return( expression );

} /* Function */
¿Qué tipos de datos retorna una función?

return-type
Function()
{
...
return( expression );
} /* Function */

Se puede retornar cualquiera de los tipos escalares (entero, flotante y apuntador).


Adicionalmente se puede retornar un tipo de dato estructura. En C no se puede
retornar ni arreglo, ni función. Sin embargo, si se puede retornar un apuntador a
arreglo y un apuntador a función.
MANY trivial functions

(and others not that trivial)


Function Always_TRUE is always true…

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 */

int main( void )


{
fprintf( stdout, “Max: %d\n”,
Maximum(a,b) );

} /* main */
Function Minimum returns minimum

int
Minimum( int x, int y )
{
return( (x < y) ? x : y );

} /* Manimum */

int main( void )


{
fprintf( stdout, “Min: %d\n”,
Minimum(a,b) );

} /* 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( int x, int y )


{
return x + y;

} /* 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_TRUE( int a, int b )


{
int add = a + b;

return 1;

} /* Return_TRUE */
Function Anticipated quits before reach end…

Anticipated( int a, int b )


{
return;
/* Nothing is executed */

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 &&

int And( int input_1, int input_2 )


{
if( input_1 && input_2 )
return 1;
else
return 0;

} /* And */
Function And using operator &&

int And( int input_1, int input_2 )


{
int output = 0;

if( input_1 && input_2 )


output = 1;

return output;

} /* And */
Function And WITHOUT using operator &&

int And( int in1, int in2 )


{
int out[] = { 0, 0, 0, 1 };

in1 = (in1 ? 2 : 0);


in2 = (in2 ? 1 : 0);

return out[in1|in2];

} /* And */
Function And WITHOUT using operator &&

int And( int in1, int in2 )


{
int index, out[] = { 0,0,0,1 };

index = (in1 ? 2 : 0);


index |= (in2 ? 1 : 0);

return out[index];

} /* And */
Function Or using operator ||

int Or( int input_1, int input_2 )


{
return input_1 || input_2;

} /* Or */
Function Xor … ¿What does this do?

int Xor( int in1, int in2 )


{
return
Or(In1,In2) && !And(In1,In2);

} /* Xor */
THE END

You might also like