beginner question about functions

Hello there! I'm an absolute beginner in programming and C++.
I'm learning about functions and wrote a piece of code, a small function with a random formula.


In the first case the function has "int" in front of it and it returns a value, which is used in the main func and printed there:


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

using namespace std;

int someCoolformula(int a, int b, int c, int d, int e)
{
    int result = ((a + e) * 3.14) - (b + c + d);
    return result;
}

int main()
{
    cout << someCoolformula(1, 2, 3, 4, 5) << endl;
    return 0;
}





and in the second case, our formula function is "void", doesn't return a value and has the output command inside itself:


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

using namespace std;

void someCoolformula(int a, int b, int c, int d, int e)
{
    int result = ((a + e) * 3.14) - (b + c + d);
    cout << result << endl;
}

int main()
{
    someCoolformula(1, 2, 3, 4, 5);
    return 0;
}




The result from both programs is the same, but I wonder is there a right or wrong way of doing it and which is the better programming practice from both examples?


---
My two cents: For more complicated programs, functions do much more than just print simple numbers to a screen. What if the user wants to actually use the number in someCoolformula? If you just print it, they can't use it for further processing. Prefer to return numbers, and let the caller of the function handle input/output, unless you specifically call your function "print_X" or something.

PS: I notice you have 3.14 in there. int stands for integer. As in {-2, -1, 0, 1, 2, 3 .. }. If you want your result to have values between integers, you want to use double result; instead of int result;

________________________________________

Edit: Once you make a large enough program, you'll inevitably start to see code duplication and similar logic. This is where factoring similar logic into a function is vital, regardless of the actual return value or lack thereof.

Aside from code duplication, functions are more than just a thing that returns another thing. Functions allow you to logically separate different "chunks" or modules of your code.
Last edited on
is there a right or wrong way of doing it


No.

It really depends on what you want your function to do.

If you just want to display some result on the console screen both are equal. On the other hand, if you want a value returned that you can store in a variable to work with later, then you need a function with a return type, or you can have a void function manipulate some variable by passing the variable by reference to the function.

Really, if you are getting the results you want then it does not matter how you do it.
Your programs perform two actions:
* compute a value
* show a value

On the second version one function does both actions. They are strongly coupled. That can be convenient, if the only reason to compute the value is to show it.

The first version is more flexible / less coupled. The function performs exactly one task: it computes a value. The caller of the function decides what to do with the value: to show, to use in another computation, ...
In other words the value-returning function is more useful / reusable.


Less is more. A function that does only one action is simpler. Less code. Less chances for errors. More ways to use.
I use two rules to help decide where to break up functions (and break the rules if it seems necessary):

1. Separate computation from presentation. In general, it's best to compute results in one place and show them to the user somewhere else. That way if you need to change how you displace the results, or where you display the results, or in what format you display the results, or in what language you display the results, then it's easy, and you don't have to worry about messing up the computation code.

2. Functions don't need to "do the work." they need to "make doing the work easy." If you think like this then you'll create functions that are a little more generic, which makes them a little more useful. A classic example is the problem of saving and restoring a class to a file. You could write void save(const string &fileName), but it's just as easy to write ostream &save(ostream &os) and have caller create an ofstream. This is so much more useful because now you can save to a string or the middle of a larger file or whatever.

For both reasons, I'd prefer your first program. Suppose that after writing the program, you were asked to modify the program to graph someCoolformula(). The first one would be easier to modify.
Thank you guys for your valuable responses! I really appreciate it.
Topic archived. No new replies allowed.