C ANSI : Function optional parameter

In C programming language (C ANSI not C++), there is a diferrent method to use optional argument for the function. The optional argument means the argument is not necesary needs by the function. In some programming languages the optional arguments always have some value, which the values has been declared before at the function header.

This is an example function in C++ that have an optional argument :

int myFunc(int a, int b, int ab=0){
	if (ab==0){
		return a+b;
	}else{
		return a+b+ab;	
	}
}

In the myFunc function header above the argument of "ab" variable has a 0 value. This mean the programmer can called the function with 2 or 3 argument, which the arguments "a" and "b" must have a value on every function call and the third argument "ab" can be empty or not. If the "ab" argument is empty when the function call it will have 0 value.

Example :

  • If we call the myFunc(1,2), it will return 3 value.
  • and if we call the myFunc(1,2,3), it will return 6 value.

C ANSI Optional argument

In C ANSI function or we can also call it C function (without the ANSI text) there is no optional argument. But if you using a printf function in C, we can put one, two or more arguments, base on the format specifier. like this :

printf("printf with no specifier format\n");
printf("printf with %s specifier format \n","one");
printf("printf with %s specifier format %s","two","arguments");

So how printf function can do that without an optional arguments? In C function we can put a list of arguments at the function header. The list of arguments is likely same as an optional argument in another language. And also the list of argument is dynamic, it can be one or more optional argument in the argument field can be put to the function without defining the number of argument.

To use the argument list we must include the header file stdarg.h at the top of our code. The function with the argument list must have at least one fix argument. The argument list can be defined with three dots (...) at the header of the function after the fix argument. For example like this function before :

#include <stdarg.h>


void myFunc2(int a, int b, ...){

	int ab;
	va_list ap;
	va_start(ap,b);
	ab=va_arg(ap,int);
	ab=ab>a ? 0 : ab;
	va_end(ap);
	if (ab==0){
		return a+b;
	}else{
		return a+b+ab;	
	}
}

In the function with argument list need a va_list variable to put the element from argument list to another variable. va_start function is used to point the va_list to the argument list. Before, we must tell the va_start function what is the last fix argument. On the example above "b" is the last fix argument before the argument list statement (...).

The va_arg function will get the argument list one by one and returned it to another variable with the specify type of data. If we call the va_arg function it will move the pointer to the next argument in the argument list. After all argument has moved to another variable with va_arg, we must call va_end function.

The example myFunc2 function it will result same as the first myFunc function implemented in C++ at the first of my note.

-Nz-

Komentar untuk blog ini :

captcha

Belum ada komentar!!