Function Output C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

Function Output C++

void calcsum(int arr[], int size){ int sum = 0; for (int x = 0; x < size; x++){ sum += arr[x];} std::cout << sum;} what should i code to the main() if i want to output this function?

11th Aug 2018, 10:01 AM
DarkEye
DarkEye - avatar
10 Answers
+ 5
DarkEye here's a sample code for you: #include <iostream> void calcsum(int arr[], int size) { int sum = 0; for (int x = 0; x < size; x++) sum += arr[x]; std::cout << sum; } int main() { // Here's an array with 5 elements int arr[] {10,20,30,40,50}; // And this is how we pass the array // note that we pass the array size (5) // as the 2nd argument in calling calcsum calcsum(arr, 5); } Hth, cmiiw
11th Aug 2018, 10:58 AM
Ipang
+ 4
Call the function, passing it an array of int, and the array size for first and second argument, respectively. Needless to say the array is expected to have values : )
11th Aug 2018, 10:06 AM
Ipang
+ 3
Well, of course it doesn't work, you passed an int value as the 1st argument, while the function expected an array of int, you need to pass an array as 1st argument. Do you have an array already or not?
11th Aug 2018, 10:18 AM
Ipang
+ 1
thanks that is what i looked for. have a nice day!
11th Aug 2018, 10:59 AM
DarkEye
DarkEye - avatar
+ 1
answer is for and x. God Jesus Bless.
13th Jan 2020, 10:39 AM
Imanuel Wicaksono
Imanuel Wicaksono - avatar
0
i know but how do i output this? for example: calcsum(3, 5); this doesn't work. i want to know what to write it correctly to output my function
11th Aug 2018, 10:08 AM
DarkEye
DarkEye - avatar
0
I'll ask that way, if I have an arr[3], what should I write in calcsum -> calcsum(? , 5);
11th Aug 2018, 10:42 AM
DarkEye
DarkEye - avatar
0
DarkEye please open my code again and let me know if your problem is solved.https://code.sololearn.com/cCXiKO2MmfI4/?ref=app
11th Aug 2018, 11:00 AM
Prabhjot Singh Sahni
Prabhjot Singh Sahni - avatar
0
my problem is solved prabhjot
11th Aug 2018, 11:00 AM
DarkEye
DarkEye - avatar
- 3
DarkEye Hope this code will help. If have any suggestions please tell me. I thankful for that https://code.sololearn.com/cCXiKO2MmfI4/?ref=app
11th Aug 2018, 10:29 AM
Prabhjot Singh Sahni
Prabhjot Singh Sahni - avatar