Default Parameter Question

Assuming a code snippet like the one below. How would you go about adding default parameters?

1
2
3
4
5
6
int *arr(int size, int val){
    int *a = new int[size];
    for (int i = 0; i < size; ++i) 
        a[i] = val;
    return a;
}


I get that the idea is this (as seen below), but I'm just not sure how to implement it exactly in the above code snippet? Is it as simple as simply adding val = some int value?
1
2
3
4
5
6
7
8
9
int foo(int a; int b = 10){
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
}

int main(){
    foo(1); // Would print a = 1 b = 10
    foo(1, 2); // Would print a = 1 b = 2
}
Last edited on

Is it as simple as simply adding val = some int value?

Yep! It's as simple as you think it is.
Oh cool, thank you!
Topic archived. No new replies allowed.