No. Printing something to the console (pushing something to cout with the << operator) is not the same as returning a value.
Think of returns like what you have in math. When you have a function, let’s say
intdouble(int n){
return2 * n;
}
that returns an int, calling it will “replace” the expression with its result, much like in math. So in this instance, doing 3 + double(2) would be like doing 3 + 4. Math is the same way; if you have a function, f(x) = 2x, doing 3 + f(2) would be the same as doing 3 + 4.
Printing to the console involves doing I/O (input/output), and actually writing a string to a file, which then the terminal can display.
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
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
Of course they aren’t the same. try to achieve the same output with int sum() instead of void sum() this small exercise will help you understand the difference
#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
int main() will return 0 if you don’t have a return statement
yes but sum() gives the value 90 in the main function. Why is that?
You’re printing s to console not returning it.
Oh… so cout << and return don’t mean the same thing.
No. Printing something to the console (pushing something to
cout
with the<<
operator) is not the same as returning a value.Think of returns like what you have in math. When you have a function, let’s say
int double(int n) { return 2 * n; }
that returns an int, calling it will “replace” the expression with its result, much like in math. So in this instance, doing
3 + double(2)
would be like doing3 + 4
. Math is the same way; if you have a function,f(x) = 2x
, doing3 + f(2)
would be the same as doing3 + 4
.Printing to the console involves doing I/O (input/output), and actually writing a string to a file, which then the terminal can display.
Thanks this is helpful
Glad to help :3
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’sprintf
or, if you can, use fmt, it’s super fast and even simpler than c’sprintf
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
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
Removed by mod
Of course they aren’t the same. try to achieve the same output with int sum() instead of void sum() this small exercise will help you understand the difference
Yes, thanks for the help