int for both program different?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio>

int subtraction (int,int);

int main ()
{
	int x=5,y=3,z;
   z=subtraction (7,2);
   cout<<"The first result is "<<z<<'\n';
   cout<<"The second result is "<<subtraction(7,2)<<endl;
   cout<<"The third result is "<<subtraction(x,y)<<endl;
   z=4+subtraction(x,y);
   cout<<"The fourth result is "<<z<<'\n';
   getch();
   return 0;
}

int subtraction(int a, int b)
{
	int r;
   r=a-b;
   return (r);
}


Hi, may I know why the above program no need declare variable for a and b inside line 4, but for the program below, if I don't declare the variable in line 4(which is same as the above example), the program cannot run?
Thanks...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <conio>

void prevnext (int, int, int);

int main()
{
	int x=100,y,z;
   prevnext (x,y,z);
   cout<<"Previous="<<y<< ", Next="<<z;
   getch ();
   return 0;
}


void prevnext (int x, int&  prev, int&  next)
{
	prev = x-1;
   next = x+1;
}
The two functions has different parameter types. In the first program you declare the function as

int subtraction (int,int);

and define it the same way

int subtraction(int a, int b)
{
...

In the second program the function declaration does not correspond to function definition

void prevnext (int, int, int);

void prevnext (int x, int& prev, int& next)
{
...

If you will declare it as

void prevnext (int x, int &, int &);

then the code will be compiled.
Thanks...

So I can do it this way without putting "x" inside it right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <conio>

void prevnext (int, int&, int&);

int main()
{
	int x=100,y,z;
   prevnext (x,y,z);
   cout<<"Previous="<<y<< ", Next="<<z;
   getch ();
   return 0;
}


void prevnext (int x, int&  prev, int&  next)
{
	prev = x-1;
   next = x+1;
}
Topic archived. No new replies allowed.