tiny question

May 27, 2015 at 4:19pm
hey everyone,
am i required to use function other than main in this question or return this values in main ? (I've already solve it)

Write a C++ program that takes a NxN 2-dimensional square
array of integers. If the summation of all array elements is odd, the function returns the
summation of the elements in the 2 diagonals of the array, otherwise it returns -1.

May 27, 2015 at 4:24pm

the function returns the summation of the elements in the 2 diagonals of the array, otherwise it returns -1.

Yes
May 27, 2015 at 4:30pm
so if i'm required to use function the code will look like this ?



int main()
{
function call (......)
}


other_function(.......)
{
if (even)
return -1
else
return sum
}
May 27, 2015 at 4:36pm

Yeah

If its even, return -1 otherwise return the result.
May 27, 2015 at 4:37pm
return function call (.....)
May 27, 2015 at 4:39pm

Yeah as Texan40 said, remember that your function should return a value so it should be declared as such. :)
May 27, 2015 at 5:07pm
texan 40

do u mean that i have to write my function call on the main return like this ?


int main()
{
return function call (......)
}

May 27, 2015 at 6:04pm
Yes, that's what I mean.
May 27, 2015 at 6:05pm
thanks a lot :))
May 27, 2015 at 8:06pm
1
2
3
4
int main()
{
return function call (......)
}

How will you know what was returned from main? Also, on some UNIX systems, only the least significant 7 bits of the return code are actually returned to the parent process. I think you may want:
cout << function_call(...) << '\n';
instead.
May 27, 2015 at 10:23pm

You need to declare the function as such to define its return type.

In this example my function returns an integer, the type is declared before someFunction

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int someFunction();

int main()
{
	int result;

	result = someFunction();
	std::cout << result;
	return 0;
}

int someFunction()
{
	return 255;  // just some example.
}
Topic archived. No new replies allowed.