f2 was not declared

Not sure why , please help !
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>
#include <iomanip>
#include<cmath>
using namespace std;
void f1() 
 {
   int x = 5;

 f2( x );
 cout << x << endl;
 } 

 void f2( int x )
 {
 x += 5;
 cout << x << endl;
 }

int main() {


f1();
	cin.get();
	cin.get();
	return 0;
	
	
}
You are trying to use f2 before the program even knows about it. If you switch f2 with f1 you will find it compiles. However you would be better served if you used function prototypes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void f1();
void f2(int);

int main() {
	f1();
	cin.get();
	cin.get();
	return 0;
}

void f1()
{
	int x = 5;

	f2(x);
	cout << x << endl;
}

void f2(int x)
{
	x += 5;
	cout << x << endl;
}
Last edited on
Thank u
Topic archived. No new replies allowed.