Whats wrong with my code 2?

So I was trying to pull a value from array num and use it in the function int addition and I think I have some punctuation errors, anyone want to further elaborate??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int addition ( int a, int b);		
{	int a;
	int b;
	//array stated here
	int num [5]= {5,10,15,20,25};
		//Giving the value in 3rd block of array (20) to variable "a" stated here. Same thing with int b
		a= num[3];
		b= num[4];
		int r;
		r=a+b;
		return (r);
	}

	int main ();
	{
		int z;
		z = addition (a,b);
		cout<<"this equals"<< z;
	return 0;
	}

Last edited on
you should put into the main() the: int a; int b;(not the parametrs,the second couple.)

and give a value,for example a=5;etc....



also i suggest to you not give same names because will be confused.
Your code won't compile as it stands.
There is a semicolon which should be removed from the first line of the definition of both main() and addition().

-- remove the semicolon from lines 1 and 14 above.

Function addition has 2 integer parameters. However, at lines 2 and 3, new local variables called a and b are defined. These should be removed.

The call of addition() at line 17 above tries to pass a and b which do not exist within the scope of main().

Here's an example of your code modified so it will compile (though it doesn't make very much sense).

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
#include <iostream>

    using namespace std;

int addition ( int a, int b)
{
    //array stated here
    int num [5]= {5,10,15,20,25};
    //Giving the value in 3rd block of array (20) to variable "a" stated here. Same thing with int b

    a= num[3];
    b= num[4];

    int r;
    r=a+b;

    return (r);
}

 //--------------------------------

int main ()
{
    int z;
    z = addition (22, 7);
    cout << "this equals" << z;

    return 0;
}


Note, no matter what values of a and b are passed to the function, the return value r will always equal 45.
Is that what you want ??


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int addition ( int a, int b) {
int c=a+b; 
return (c);}

int main () {
int z=addition (3,5);
cout << "result: " << z << endl;
return 0; }
Last edited on
there are no errors while picking up a value for a and b from the array but you have to do it inside int main().
Topic archived. No new replies allowed.