I need to calculate the value of sin(sin...(sin(x))) for n times (n and the value of sin are input from the keyboard).
That's is what I've came up with.

#include "stdafx.h"
#include <iostream>
#include <math.h>
using namespace std;
double multipleSin(int n, double x);
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{double x,k; int n;
    cout<<"x= ";
cin>>x; cout<<"n= "; cin>>n;
cout<<multipleSin<<' ';
//k=sin(x);
//cout<<"check  "<<k<<endl;
system("pause");
    return 0;
}

double multipleSin(int n, double x)
{
if(n>1)
x=multipleSin(n-1,x);
else
return sin(x);
}

The probles seems to be fairly simple but I can't figure it out though.
Any advise how to make this work properly?

It is commonly taught that you can only have one (1) return statement in a function. This is not true, it's a technically-inaccurate educational device designed by instructors and included in many assignments to stress a different, yet marginally related, point. Usually, what is actually meant by teaching this is that only one (1) value can be returned from any particular function. It is also sometimes used to force the student to re-structure their program(s) in a certain way.

That being said, a function can have more than 1 return statement. What you need to keep in mind is that which return statement executes (and therefore the value that is returned) must be controlled by the logic within the function.

Have another look at your function (re-formatted for readability):

double multipleSin(int n, double x)
{
  if(n>1)
    x=multipleSin(n-1,x);
  else
    return sin(x);
}

What is wrong with the flow of control through the if statement? Can you see an issue with the return? What happens after a recursion is executed?

In answer to the subject line - 'how does recursion work'.

Recursion causes a new function call to be pushed onto your call stack. You may visualise it procedurally as a function which calls a function which calls a function etc. For example, an un-rolled "factorial" function would look like:

int factorial1()
{
    return 1;
}

int factorial2()
{
    return 2 * factorial1();
}

int factorial3()
{
    return 3 * factorial2();
}

int factorial4()
{
    return 4 * factorial3();
}

int factorial5()
{
    return 5 * factorial4();
}

int factorial6()
{
    return 6 * factorial5();
}

#include <iostream>

int main()
{
    std::cout << factorial6() << std::endl;
}

You can see that each of the "factorialX" do exactly the same thing with different numbers - essentially this is what an un-rolled recursive factorial function looks like; factorial6() calls factorial5(), which calls factorial4(), which in turn calls factorial3(), etc.


If you were able to examine the function call stack for the above program, it would look more-or-less exactly the same here in this recursive example

int factorial(int n)
{
    if(n > 1)
    {
        return n * factorial(n-1);
    }
    return n;
}

#include <iostream>

int main()
{
    std::cout << factorial(6) << std::endl;
}

factorial(6) calls factorial(5) which in turn calls factorial(4) which in turn calls factorial(3), etc.

In summary, when a function calls itself, what happens is no different to calling another completely different function; recursion is not a 'goto' within a function it is a function call which preserves the state of its caller.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.