C++ - Optional function arguments / parameters

10. May 2012 13:00

 

This is a quick tip in C++ for providing support for optional parameters on functions. This works because when a function is declared in C++ it can be given a default value and when that paramater is missing from the calling function the compiler will automatically add it during compile time and pass the default value.

 

Heres a simple example of this.

 

 

#include <stdio.h>

bool func(bool val = false) {
    return val;
}


int main(int argc, char **argv) {

    printf("%d\n", func());
    printf("%d\n", func(false));
    printf("%d\n", func(true));

    return 0;
}

 

E-mail Kick it! DZone it! del.icio.us Permalink


Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading