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 === |
| 343 | The 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 === |
| 346 | If 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 | |
| 348 | These functions needs to have at least one argument passed. |
| 349 | |
| 350 | Steps 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 */ |
| 360 | int 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 | |