Click to See Complete Forum and Search --> : Default arguments
truthbajaj
Jan 20th, 2002, 04:04 AM
My problem is that I want to call a fuction with an array as its parameter and I want it to display entiore contents of that array if I do not send any other parameter but if I send an index as another parameter, it should display only the contents of that index. This can be done through Default Parameter as O but it is not working correctly. How can I successfully do this?
parksie
Jan 20th, 2002, 05:08 AM
void func(int *ptr, int sz, int idx = -1) {
if(idx != -1 && idx >= 0 && idx < sz) {
cout << ptr[idx] << endl;
} else {
for(int i = 0; i < sz; ++i) {
cout << ptr[i] << endl;
}
}
}Is that sort of what you mean?
kedaman
Jan 20th, 2002, 01:40 PM
Originally posted by parksie
idx != -1 && idx >= 0
Mr. Parks! You should take some logic lessons soon!
parksie
Jan 20th, 2002, 03:29 PM
I think I'll take them now...oops ;)
I'd started with != -1 then decided to put a check for anything out of bounds and forgot the -1 was still there...
kedaman
Jan 20th, 2002, 03:37 PM
My problem is that I want to call a fuction with an array as its parameter and I want it to display entiore contents of that array if I do not send any other parameter but if I send an index as another parameter, it should display only the contents of that index. This can be done through Default Parameter as O but it is not working correctly. How can I successfully do this?
Why do you want to do this? VB habit? Get rid of immediately. For two different operations (by two different calls) have two different functions, ex displayall and displayone
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.