A function is C++ is a block of code; you sometimes feed it some parameters, and you sometimes get something back. For example, here's a simple function that adds two numbers and gives back (returns) a number:
int addTwoNumbers (int a, int b)
int sum = a+b;
return sum; … Read More
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. Read More
Just to make the point more clear for the OP,
Consider my program having following steps:
1. Add a+b;
3. Hit Jack in the Head
4. Try to make noodles.
5. Again a+b.
6. Return 0;
Now if my code works fine and complete successfully, the step 6 … Read More
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. … Read More
> 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 … Read More
Adding to what's already said, here's a description from the specification (section 6.6.3):
A function returns to its caller by the return statement.
A return statement without an expression can be used only in functions that do not return a value, that is, a function with the return type void, … Read More