0

I'm a freshman in IT and we're currently discussing functions in C++. I just want to ask the difference between our prof's code and the other code that I tried.

This is the sample code our prof showed us:

#include<iostream>      //header file
using namespace std;

  int num1, num2, sum = 0;   // global variable
  
  int addTwoNumbers(int a, int b)
  {
      sum = a + b;
      
      return sum;
  }

 int main()
  { 
    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: "; 
    cin >> num2;
  
    sum = addTwoNumbers(num1, num2);
    cout << "\nThe sum is  " << sum;
  }

and as for the code I tried, I simply removed the "sum =" part. So,

addTwoNumbers (num1, num2);
cout << "\nThe sum is " << sum;

and it still did the same thing. At least, from what I saw in the output. Is there any difference between the two behind the scenes or is there really nothing?

celle
  • 23
  • 3
  • 2
    It's working because of your global variables. The short answer is that yes there is a difference, and I'll add on that the first person you should be asking is your professor. – sweenish Sep 05 '22 at 01:23
  • 4
    That function should be a one-liner (or not exist at all), and the variable `sum` shouldn't exist. `num1` and `num2` should be declared in your main function. `using namespace std;` is a bad practice and you should stop it sooner than later. – sweenish Sep 05 '22 at 01:24
  • `sum` is a global variable, you already assign the summary result to it directly in function `addTwoNumbers` so another assignment in `main` is duplicated. It is a good example to show that global variables should be avoided as possible. – Zongru Zhan Sep 05 '22 at 02:12

2 Answers2

3

The 1st code is ... confusing. I hope your professor didn't show this code to introduce you to functions, but to rather quiz your already knowledge of functions and global variables.

The confusing part are the global variables and how they are used inside the function. If we remove them and forget about them completely the code is better suited to teach you about functions. Let's see:

#include <iostream>

int addTwoNumbers(int a, int b)
{
    int sum = a + b;
    return sum;

    // or simply:
    // return a + b;
}

int main()
{
    int num1, num2;
    std::cout << "Enter first number: ";
    std::cin >> num1;
    std::cout << "Enter second number: ";
    std::cin >> num2;

    int sum = addTwoNumbers(num1, num2);
    std::cout << "\nThe sum is  " << sum;
}

Now there are no hidden dependencies between main and addTwoNumbers (in the form of the global variables). This illustrates the procedure of passing data to function and getting data back from the function (parameters and return). Analyze this code and understand how it works. Play with it.

Now this won't work:

addTwoNumbers (num1, num2);
cout << "\nThe sum is " << sum;

Because the only way data escapes the function is via its return. Since you discard the return value (you don't assign the result of calling the function) you don't have the result of the sum.

Now you could say you could do this instead:

int sum = num1 + num2;
cout << "\nThe sum is " << sum;

and ask "what's the point of functions?"

True for this particular small function there is no practical point of having it. You'll always write the num1 + num2 instead. However its simplicity makes it perfect as a teaching tool. You will soon see functions that get more complex and you will learn (either by being told or learning yourself the hard way) that writing code in very small chinks (functions) is better for both writing (breaking down the big problem in smaller problems) as well as for reading.

bolov
  • 72,283
  • 15
  • 145
  • 224
  • Ohh, I see! Thank you for the explanation, it really helped me understand. I'm not sure why, but my professor only knows about the basics of c++. When I asked, they couldn't really answer. I kind of expected it since there is a lack of quality education in my country, especially in my field. I'm self-studying to compensate for that. – celle Sep 05 '22 at 23:48
  • 1
    @celle you are welcome. Good for you, even with good teachers without self studying you can't get far. Please don't fall victim to online sites that promise to teach you C++ in x days. Most of them are rubbish . Do get a good book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – bolov Sep 06 '22 at 00:02
1

First of all, the reason why the sum is returning those values is that you are assigning the sum to itself. Basically, the addTwoNumber() returns the sum, and then you are assigning that value back into the sum. Hence, you don't need to assign the sum again in other words (sum = addTwoNumbers is unnecessary).

Yes, your code is working and it is actually better than the teachers in this case. However, your teacher may want to show you that you can use global variables like this. Typically you would store that value in another variable for later use if needed.

  • Also bro ignore these other people... you can always change a line of code like "using namespace std" so I don't think it is a bad habit. Once you learn more and understand why you should write std::cout and not use the namespace then you can do that and incorporate it into your behavior. – AceJoker Studio Sep 05 '22 at 01:41
  • Given that I'm the "other people," I'll just explain that as a professor, I used to teach with that line of code in Intro. Once we moved on to OOP and had to ditch it, more than zero students every semester had a very hard time doing so. Ditch it early, it's a bad habit. Or is it okay to smoke until you learn more about smoking? – sweenish Sep 05 '22 at 01:56
  • I'll also throw out that "better" between the two choices does not equate to good. – sweenish Sep 05 '22 at 01:57