And for whatever reason, 0 is traditionally the value a program returns upon normal completion. You can return other values and for the purposes of your program define what they mean, perhaps indicating various error conditions that caused the program to not complete normally.
vmanes
Postaholic
2,019 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 243
Skill Endorsements: 6
Programs can run other programs, for example I could write a program that runs PrimePackster's program. So if his program was supposed to hit jack in the head, but missed and returned an error number from main() instead of 0 then my program would know that and take some action. In that instance the operating system passes the return value of main() from PrimePackster's program to my program. The return value from main() can also be used in batch files (MS-Windows) or shello programs (*nix).
Ancient Dragon
Achieved Level 70
32,269 posts since Aug 2005
Reputation Points: 5,852
Solved Threads: 2,590
Skill Endorsements: 70
When I hear function I think about maths, with f(x)= 2x + 4.
Computer science and programming are rooted in mathematics, so that's not a bad way to approach the concepts. Taking your example, it translates easily to C++ like so:
int f(int x)
{
return 2 * x + 4;
}
And by providing x, the caller can execute the function:
int main()
{
std::cout << f(5) << '\n'; // 2 * 5 + 4
}
What is a function's caller?
The code that executes a function, provides arguments, and accepts the return value is the caller. In the example above, main() is the caller for f(), and f() is the callee.
deceptikon
Challenge Accepted
3,499 posts since Jan 2012
Reputation Points: 822
Solved Threads: 481
Skill Endorsements: 58
So the 2* x + 4 is the value that is returned to the function (fx), right?
Yes.
The int does it stand for integer as in the numbers 1,-1,2,-2,2,-3,...?
Yes.
deceptikon
Challenge Accepted
3,499 posts since Jan 2012
Reputation Points: 822
Solved Threads: 481
Skill Endorsements: 58
Question Answered as of 3 Months Ago by
PrimePackster,
deceptikon,
Ancient Dragon
and 3 others