@pancake
link
fedilink
510M

It could also modify memory. If the function writes to a global variable or to an address passed as a parameter, then it makes a difference. For example, would you say the void function ‘free()’ is useless?

@the_tech_beast
creator
link
fedilink
110M

Thanks

You use functions to extract code that does a specific thing to encapsulate it. So when you want to do a specific thing at three different places in your program, you don’t need to write the same code three times but you call the function. It avoids redundancy and this is easier to maintain (only one place to fix stuff).

@the_tech_beast
creator
link
fedilink
210M

Yes, got it😀___

@HMH
link
fedilink
210M

Side effects, it could print something for example.

@the_tech_beast
creator
link
fedilink
110M

could you give an example?

@OhScee
link
fedilink
2
edit-2
2M

deleted by creator

@the_tech_beast
creator
link
fedilink
110M

Hmm the syntax is different…

Is this C++ or Python?

@OhScee
link
fedilink
3
edit-2
2M

deleted by creator

@the_tech_beast
creator
link
fedilink
110M

Thanks

This thread is great, it’s asking a question i didn’t know to ask, and giving answers that are clarifying years of my own mistakes

@the_tech_beast
creator
link
fedilink
110M
#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
link
fedilink
410M

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

@the_tech_beast
creator
link
fedilink
110M

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

LunaticHacker
link
fedilink
310M

You’re printing s to console not returning it.

@the_tech_beast
creator
link
fedilink
110M

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

@ChinaNumberOne
banned
link
fedilink
510M

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

@camccar
link
fedilink
110M

I don’t think it’s necessary to learn c first. There is a fmt package if you like the way go prints more.

Tmpod
link
fedilink
510M

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 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.

@the_tech_beast
creator
link
fedilink
110M

Thanks this is helpful

Tmpod
link
fedilink
110M

Glad to help :3

LunaticHacker
link
fedilink
210M

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

@the_tech_beast
creator
link
fedilink
110M

Yes, thanks for the help

  • 0 users online
  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 5 users / 6 months
  • 441 subscribers
  • 154 Posts
  • 51 Comments
  • Modlog