function help

closed account (i8bjz8AR)
Trying to write a function that takes two parameters. Getting some errors:

Here is the function

1
2
3
4
5
6
7
8
9
10
11
two_parameters(int a, int b){

		 		int total;

		 		cin >> a;
		 		cin >> b;
		 		
				a + b = total;

		 		std::cout << total << std::endl;
		 	}


Here is how I am calling it in int main:

two_parameters(int , int);

And this is my function prototype at the top: int two_parameters(int, int);
What do you think the values of int and int are where you are calling it in int main? When you use a function that expects parameters, you should pass it variables that have the same corresponding type. Take a look at the addition function here: http://www.cplusplus.com/doc/tutorial/functions/

With your function body as it is, you could just declare int a and int b as local variables instead of as a paramater.

Your function is also missing a return type. Even if you plan to not return anything, you must put void.

I think you should take a gander at the tutorial I linked to above, so that you can understand how functions work in C++ a bit better.
closed account (48T7M4Gy)
If you have a function which takes two parameters then it would normally look something like

1
2
3
4
5
6
int function ( int anumber, int bnumber)
{
   int answer = 0;
   answer = anumber + bnumber;
   return answer;
}


Prototype is just the first line without variable names ( not mandatory to remove names but should be)

In main:
1
2
3
4
5
6
7
int a = 5; // ie no cin required inside the function because these are passed on to it
int bob =6;

int result = 0;

result = function(a, bob);
etc
Topic archived. No new replies allowed.