Good evening. The question is how to assign the values ​​of the variables a and b from the main'a function to sum. I can not figure it out enter image description here

  • four
    The code should be put in the form of code, and not in the form of a screenshot. Not the first question already. - VladD

2 answers 2

Let me confuse you completely so that you have something to read. :)

First, you should try to write functions so that they perform one specific action. Your function sums up two numbers and prints them to the console. If you just need to get the sum of two numbers, then you have to write another function. This could have been avoided if your function only calculated the sum of two numbers and returned it to the calling code.

Another problem with your function is that the sum of two numbers of type int may not fit in an object of this type, that is, there may be an overflow. Then in this case the function will give an incorrect result.

In principle, a function can be defined in two ways. The first method according to the number of parameters corresponds to your function declaration. But this is the only thing with which it is similar to the definition of the function that I showed in the next demo program. I specifically used the sum name collision so that, as I said, you had something to read. :)

 #include <iostream> #include <limits> void sum( long long int &sum, int a, int b ) { sum = static_cast<long long int>( a ) + b; } int main() { long long int sum; ::sum( sum, std::numeric_limits<int>::max(), std::numeric_limits<int>::max() ); std::cout << "sum of " << std::numeric_limits<int>::max() << " + " << std::numeric_limits<int>::max() << " is equal to " << sum << std::endl; return 0; } 

The program output to the console next

 sum of 2147483647 + 2147483647 is equal to 4294967294 

In programming languages, the parameter of the function declared as long long int &sum , that is, declared as a reference, is called the output parameter. Instead of a link, you could also use a pointer as the first parameter.

The second simpler and more natural function declaration might look like this.

 #include <iostream> #include <limits> long long int sum( int a, int b ) { return static_cast<long long int>( a ) + b; } int main() { long long int sum; sum = ::sum( std::numeric_limits<int>::max(), std::numeric_limits<int>::max() ); std::cout << "sum of " << std::numeric_limits<int>::max() << " + " << std::numeric_limits<int>::max() << " is equal to " << sum << std::endl; return 0; } 

Here, the value in the calling code is passed through the return value from the function.

In both cases, the result is the long long int type, which avoids overflow, as can be seen from the console output.

As for your program, keep in mind that the proposal in main

 void sum(); 

is a local declaration of a function named sum , which has no parameters and has the type of returning void . This local declaration hides the declaration of another sum function declared before main .

  • Somehow this is all too beautiful and difficult for a similar question ... - andy.37
  • one
    @ andy.37 I wrote this in order not to repeat with the previous answer. So that the author of the question has new interesting information. :) - Vlad from Moscow
  • I am not sure that the person asking such a question needs such information (at this stage). There would be to understand the basics, for example, why the code in the question should not be compiled. What are local and global variables. However, the answer is good, + - andy.37
  • Hmm And why you in the first fragment of the code long long int sum specify. After all, it is outside the function => does not work. Please explain. - Men's Phisique
  • @ Men'sPhisique The first function has three parameters, and it is passed three arguments, respectively: the variable variable declared in main, and a link to it, and two expressions that give the maximum value for an int number. Since the first parameter is a reference, changing it in a function leads to a change in the variable declared in main. - Vlad from Moscow

In my opinion, better like this:

 #include <iostream> using namespace std; void sum(int a, int b) { int sum = a + b; cout << sum; } int main() { int a = 3; int b = 5; sum(a, b); return 0; } 

In this case, the variables a and b in the sum function are not assigned, but are passed as parameters. The variables a and b in the main function are completely different variables. When you make a call to the sum function, the values ​​of the variables a and b copied to the stack frame of the sum function. Further the program works only with them. There is no point in setting the parameter of the function sum , because changing this variable inside the function will not affect its value outside the function, as I repeat, this is a completely different variable.

There is a second option: to return the value from the function, and output the result in the function main . It will look like this:

 #include <iostream> using namespace std; int sum(int a, int b) { return a + b; } int main() { int a = 3; int b = 5; int s = sum(a, b); cout << s << endl; return 0; } 
  • Yes, everything worked out. But why when we removed the sum, did it work? - Men's Phisique
  • Completed the answer. - maestro
  • On which topics to read the information, so that henceforth there are no questions? - Men's Phisique 5:58 pm
  • There will always be questions. These things are always explained at the beginning of any textbook. Read the material carefully, understand, do the tasks, practice. That's all programming. - maestro
  • And it seems that the variable s should be written instead of sum in the 12th line, right? - Men's Phisique