+ 1
What if you want to add a varying number of inputs to the function. i.e. you want it to perform addition for 1, 2, 3 , 4 or 5
Cpp
7 ответов
+ 1
How
I'm c++
+ 1
What if I want the user to vary the size
Say for example
A function where the user puts in any number of inputs. And it gives the sum of all and subtracts the last
0
You can use a table.
0
int myFunc(int arr[]) {
}
0
However, you may also want to pass the size so
int myFunc(int arr[], int size) {
}
0
I'm not that good in cpp but I'll assist you. It seems you're trying to add number 1 to 5 within a function, if that's the case then try this out:
a =1 and b = 2 which would be passed to the function as shown
int myFunc(int a, int b) {
int c;
c = a +b;
while (b < 5){
b++;
c = c + b;
}
return c;
}
So within your main function just send in the values
int main() {
int value1= 1;
int value2 = 2;
myFunc(value1, value2);
}
Hope this helps.