What is the use of a void function if it doesn't return a value?

1 points · 7 comments · view on lemmy.world

7 Comments

pancake@lemmy.ml · 1 pts · 4y
[ removed ]
the_tech_beast@lemmy.ml · 0 pts · 4y (6 replies)
#include <iostream>
using namespace std;
void sum()
{
    int a, b, s;
    cin >> a >> b;
    s = a + b;
    cout << s;
}
int main()
{
    sum();
}

Also why does this function return a value?

for example, a = 45 and b = 45

I get the sum as 90

lunatichacker@lemmy.ml · 0 pts · 4y (5 replies)

int main() will return 0 if you don't have a return statement

the_tech_beast@lemmy.ml · 0 pts · 4y (4 replies)

yes but sum() gives the value 90 in the main function. Why is that?

lunatichacker@lemmy.ml · 0 pts · 4y (3 replies)

You're printing s to console not returning it.

the_tech_beast@lemmy.ml · 0 pts · 4y (2 replies)

Oh... so cout << and return don't mean the same thing.

ChinaNumberOne@lemmy.ml · 1 pts · 4y (1 reply)

don't know if you are a beginner to programming in general or c/c++ specifically but it's better to start with c before c++, it's simpler and clearer (than c++) to a beginner

then cout syntax is absolutely horrible and very misleading, use c's printf or, if you can, use fmt, it's super fast and even simpler than c's printf

glibg10b@lemmy.ml · 2 pts · 3y

If you want to learn C++, you should start with C++. Starting with C will form unsafe habits and teach unsafe paradigms that have been replaced by language features or parts of the C++ standard library

it's simpler and clearer (than c++) to a beginner

A language only seems as clear as the tutorial used to teach it. If you think the basics of C++ can be better taught using a C tutorial,. you've been looking at the wrong C++ tutorials. Transitioning from C to C++ will be a confusing process for a beginner