# Default argument like c++

**URL:** <https://discourse.processing.org/t/default-argument-like-c/8131>\
**Category:** Coding Questions\
**Created:** [February 5, 2019, 6:54am UTC](https://discourse.processing.org/t/default-argument-like-c/8131 "2019-02-05T06:54:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![diazdaiz](https://avatars.discourse-cdn.com/v4/letter/d/bc8723/32.png) [@diazdaiz](https://discourse.processing.org/u/diazdaiz)\
**Post date:** [February 5, 2019, 6:54am UTC](https://discourse.processing.org/t/default-argument-like-c/8131/1 "2019-02-05T06:54:36Z")

</div>

is there a way so i can write default argument in processing like c++?  
in c++, for example:

void test(a=100)  
{  
… //some function that print value of a  
}

so, when we call the function of test, if we dont passed value to that function, it will print 100  
for example:  
test() //print 100  
test(200) //print 200

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [February 5, 2019, 9:03am UTC](https://discourse.processing.org/t/default-argument-like-c/8131/2 "2019-02-05T09:03:51Z")

</div>

yes, looks little bit stupid, but works:

```auto
void setup() {
  test();
  test(200);
}

void test(){
 test(100);
}
void test(int a){
  println(a);
}

```

shows:

```auto
100
200

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 6, 2019, 7:42pm UTC](https://discourse.processing.org/t/default-argument-like-c/8131/3 "2019-02-06T19:42:27Z")

</div>

> [@diazdaiz](#):
>
> is there a way so i can write default argument in processing like c++?

Java does not have built-in default arguments.

@kll demonstrated one approach – method overloading, in which one signature has no arguments, and calls the second signature while supplying the default argument.

There are other approaches. One is to test for null and, if null, assign a default value. Then the no-arg form of a method call is `foo(null)`. Another approach is to use varargs, and likewise check if each arg is present, and if not, assign its default. Another is to use the Builder pattern, in which each optional argument is a method.

A good summary of six approaches to default handling is in this answer:

> <https://stackoverflow.com/questions/997482/does-java-support-default-parameter-values/19719701#19719701>
