Changes between Version 44 and Version 45 of ResumedC


Ignore:
Timestamp:
Sep 9, 2009, 10:02:35 PM (15 years ago)
Author:
Thanatermesis
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ResumedC

    v44 v45  
    305305
    306306
     307=== Prototype of Functions ===
     308Used for the header files, it contains the description of the function and how it should be used, the arguments after the variable type can be any name, used as a simple descriptive comment.
     309
     310When there's no arguments for the function, just put a ''void'' itself inside the (), so for the content of arguments.
     311
     312{{{
     313#!C
     314double calc( double base, double exponent );
     315}}}
     316
     317
    307318=== Structure of a function ===
    308319{{{
     
    329340
    330341
    331 === Prototype of Functions ===
    332 When there's no arguments for the function, just put a ''void'' itself inside the (), so for the content of arguments.
    333 
    334 When there's arguments, you should add too a name of variable after it, the mean of this is just to be more easy to understand when reading the headers file for example
    335 {{{
    336 #!C
    337 double calc( double base, double exponent);
    338 }}}
     342=== Inline ===
     343The functions can be defined as ''inline'', on such case the compilator will optimize the speed of call of the function. If the function contains too much instructions, the inline value will be ignored by the compilator.
     344
     345=== Multiple arguments ===
     346If you need to pass multiple arguments to your function, you can use the special macros from ''stdarg.h'' called ''va_start'', ''va_arg'', and ''va_end''.
     347
     348These functions needs to have at least one argument passed.
     349
     350Steps needed for use it:
     351 1. Declare an object of type '''va_list'''. ''(Example used: arglist)''
     352 1. Call the macro '''va_start''' with 2 parameters; the object ''arglist'' list, and the name of the obliged argument.
     353 1. Call the macro '''va_arg''' with 2 parameters; the object ''arglist'' so for obtain every argument passed, and the type of the obliged argument.
     354 1. Call the macro '''va_end''' before to continue with the instructions of the function, the only parameter to pass to it is ''arglist''
     355
     356{{{
     357#!C
     358#include <stdarg.h>
     359    /* In the prototype function you need to declare it with 3 dots */
     360int max(  int prem, ... )
     361{
     362  int maxarg, arg;
     363  va_list arglist;            // Object 'list' of extra arguments
     364  va_start( arglist, prem );  // We obtain the first argument
     365  arg = maxarg = prem;
     366
     367    /* The last parameter passed to the function needs to be 0  */
     368    /* so that when we reach it, the loop finshes  */
     369  while ( arg != 0 ) 
     370  {
     371    arg = va_arg( arglist, int );  // obtain an argument to arg
     372    if ( arg > maxarg ) maxarg = arg;
     373  }
     374  va_end( arglist );
     375  return maxarg;
     376}
     377}}}
     378
    339379
    340380