Calling A Function From Another Function

Simple question I know you avid programmers can answer. :)

Let's say I have a function called "distance" that computes the distance between two points.

What would I do if I want to make another function called "radius" and have that function call the distance function and return a value that would essentially be the radius?

I want to know this because I am designing a simple program that will compute the radius of a circle given the center and another point on the circle.
Last edited on
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <windows.h>
#include <iostream>
#include <cmath>

template<class _Ty> class vec2 {
public :
	vec2() {x=y=0.0;}
	vec2(_Ty X, _Ty Y) {x=X; y=Y;}
	vec2 operator+(const vec2& vec) {return vec2(x+vec.x, y+vec.y);}
	vec2 operator-(const vec2& vec) {return vec2(x-vec.x, y-vec.y);}
	vec2 operator*(const vec2& vec) {return vec2(x*vec.x, y*vec.y);}

	_Ty x, y;
};

double distance(const vec2<double>& point1, const vec2<double>& point2) {
	return sqrt(pow(point2.x - point1.x, 2) + pow(point2.y - point1.y, 2));
}

int main(int argc, char** argv) {
	SetConsoleTitleA("Radius");

	/*
	To compute the radius of a circle, you need either:
		The center point of the circle and one other point which lies on its circumference.
		Three points which lie on its circumference.
		If I recall correctly, there's some other possibilities as well.

	To find a point which lies on a circle's circumference, you need:
		The center point of the circle and the radius.
	*/
	/*Example program of finding the radius, given the center(0, 0) and another point on the circumference.*/

	vec2<double> center, point(5.0, 7.8);
	double rad = distance(center, point);
	std::cout << "The radius of the circle is " << rad << std::endl;

	/*Example program of finding a point on the circumference, given the radius and the center(0, 0).*/

	double deg = 0.0;//some random angle.
	double rad = 1.0;//unit circle.
	vec2<double> center, point(cos(deg)*rad, sin(deg)*rad);
	std::cout << "The point coordinates are [" << point.x << ", " << point.y << "]" << std::endl;

	/**/

	std::cin.get();
	return 0;
}
Last edited on
@xismn Seriously dude? WTH did you just do up there? A beginner came to the forum to ask how a function can call another function from within itself! What did you notice from the question? He's a complete beginner! And the next thing you did was write a solution with template meta-programming. Why do y'all act like this? He's still tryna figure out how to do function calls, and then you wrote bullshit for him. Tell me, when you were learning--was that how you learned? From functions to generic programming styles? What are you showing? You know C++ and you want people to know you do? Really dude? Is that it? Honestly this forum don't need people like you.
@TParker, this is how to do a function call, example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//simple function call
#include<iostream>

int add(int a, int b)
{
// a and b are the parameters in the add function
int c = a + b;
// c is the result of the addition
return c;
}

int main()
{
int x(12), y(14);
/*now here, values in x and y are substituted for
the values in a and b in function add respectively
*/
std::cout<<add(x, y)<<std::endl;
return 0;
}


Here's what you want to do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

int do_the_addition(int a, int b)
{
return a+b;
}
int add(int a, int b)
{
//this function would call another function
int c = do_the_addition(a, b);
return c;
}

int main()
{
int t(20), p(30);
std::cout<<add(t, p)<<std::endl;
return 0;
}
Sorry that I struck a nerve, Matri X.
I don't really know what makes you say that the OP is a
complete beginner
. Sure, this is his first post, but other than that there's no real way of knowing his skill set. He does seem to have some trigonometric capability, after all.

I found the OPs questioning to be a little confusing, as it's pretty straight-forward to find the radius of a circle given the center and any other point on its circumference. Prehaps, what he meant was how he should go about finding another point on the circumference, which I've also documented in the code I've provided.

@TParker, But, if I'm the sadist that Matri X makes me out to be, feel free to tell me it's too confusing or imposing.
Last edited on
Topic archived. No new replies allowed.