How to get values from a function?

The question says to get the returned values from functions and print it in main.
The final output should be
5
6.2832
3.1416

1. Is the first output the best way to do that in my code
2. How do I output the other 2?

Question in my tutorial

Write a C++ program
a. Create two functions.
i. First function will return number 5 to main function.
ii. Second function will define pi as a constant value of type double
3.1416 and returns pi* 2 to main function.
b. Define each function in namespace
i. Namespace n1 holds first function
ii. Namespace n2 holds second function
c. Call these function inside main function
d. Print pi value inside main function
Output:
5
6.2832
3.1416


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
  namespace n1 {
	int function1 () {
		return 5;
	}
}

namespace n2 {
	int function2 () {
		const double pi=3.1416;
		return pi*2;
	}
}

int main () {
	{
	using namespace n1;
	int p;
	p=function1();
	cout<<p;
	}
	
	{
	using namespace n2;
	cout<<
	}
}
Perhaps:

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

namespace ns1 {
	int function1() {
		return 5;
	}
}

namespace ns2 {
	const double pi {3.1416};

	double function2() {
		return pi * 2;
	}
}

int main()
{
	std::cout << ns1::function1() << '\n';
	std::cout << ns2::function2() << '\n';
	std::cout << ns2::pi << '\n';
}



5
6.2832
3.1416

Ah. I'd like to ask each part separately. Im testing this one

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

namespace n1 {
	int function1 () {
		return 5;
	}
}

int main () {
	n1::function1();
}


Why does it not output 5?
Because you're not outputting anything! You're calling n1::function1() but not displaying the returned value using cout.

oh. wow. i cant believe i missed that.. Sorry. Hahahaha
Understood. Thank you very much.
Topic archived. No new replies allowed.