Function practive

So in the tutorial section for functions most of the values passed between functions are constant, I want to make a program that ask the user for two values to add and use another function to add them together. No matter what numbers I enter I get 2 as the result. What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  # include <iostream>

using namespace std;

/*This program ask the user for two varables (a and b) to add together*/

int addition (int c, int a, int b)
//Function used for addition
{
    
  
      c=a+b;
    
   return c=a+b; //returns the result for addition

}

int main ()
{
    
    int a;
     int b;
      
      int result;
cout << "Please enter in your first number" << endl;
   
   cin >> a ;
   
    cout << "Please enter in your second number" << endl;
    
    cin >> b;
    
      
    int addition (result); // calls addition function
    
      cout << "Your result is" << a << "+" << b << "=" << addition <<endl;  
   
   

system ("Pause");
    
}
1
2
3
4
5
6
7
8
9
10
11
12
int addition(int a, int b) // Your 'c' variable isn't needed here
{
    return a + b;
}

int main()
{
    int a, b, result;
    // ... (prompt for a and b) ...
    result = addition(a, b); // Use the return value by assigning it to a variable
    cout << "Your result is" << a << "+" << b << "=" << result << endl;
}
Last edited on
Topic archived. No new replies allowed.