I need to understand the folowing code

return(fab(n-1)+fab(n-2));

As most of u will be fimiliar with this itz to make fabonachi series in C but can any one tell me How the computer Reads it i mean how is the sontrol transfered plzzz

Recommended Answers

All 18 Replies

What do you mean???

The above code returns sum of fab()'s return value for n - 1 and n - 2...

by the way... the correct way to wrap your code is using CODE tags

this looks like a simple recursive function, and that piece of code is the part where it returns the next value of the fibonacci series... i suppose before that there is a "cout<<" that calls the recursive function?

>but can any one tell me How the computer Reads it i mean how is the sontrol transfered plzzz
Do a google search for "recursive fibonacci trace". Most of the pages will have at least one image showing you transfer of control within the recursive path.

can any one tell me How the computer Reads it i mean how is the sontrol transfered plzzz

I think it would be better 4 you if you first learned how to use recursive functions b4 you tried one yourself... or is this some sort of benchmarking?

You had better look for the explanation in books, or internet. This recursive function you talk about is quite common in programming. So it will be definitely there in any programming book. You'll find a neat and nifty explanation there.

By the way, it's fibonacci, not fabonachi.

Thanx all for concern

I will try to understand it Through Google


Thnx Again

Well I have made the Program but there is a problem What ever value i enter i answers 0

Can any one point out The problem in COde

#include<stdio.h>
#include<conio.h>
int fab (int num);
void main ()
{
int num;
clrscr();
printf("Please enter the no yo which You want Fabbnicci series");
scanf("%d",&num);
printf("%d",fab(num));
getch();
}
int fab (int num)
{
if(num==0 | num==1)
return(0);
else
return(fab(num-1)+fab(num-2));
}

Close. If num is 0 you return 0, but if num is 1 you return 1, not 0. Also, | is not the same as ||. The former is a bitwise OR and the latter is a logical OR:

int fab ( int num )
{
  if ( num==0 || num==1 )
    return num;
  else
    return fab ( num - 1 ) + fab ( num - 2 );
}

>#include<conio.h>
You don't need this and it destroys the portability of your code. Get rid of it until you know how to use it properly.

>void main ()
This is never correct. The correct definition of main is:

int main ( void ) {
  /* Your code here */
  return 0;
}

>clrscr();
This is unnecessary when working from an IDE and extremely antisocial when working from the command line. In other words, get rid of it until you know how to use it properly.

>printf("Please enter the no yo which You want Fabbnicci series");
It's Fibonacci, not Fabbnicci. You've been told this already. Know how to spell the things you use because you can't do effective research otherwise.

>scanf("%d",&num);
Always error check your input.

>getch();
This is unnecessary when working from the command line and can easily be replaced with a standard solution when working from an IDE. Use getchar instead, it's portable at least.

I didnt Understand can u plzz explain why it prints 0

>can u plzz explain why it prints 0
What's 0 + 0? That's why it prints 0. :icon_rolleyes:

Can u please Correct it I have tried but cant do it

Also, | is not the same as ||. The former is a bitwise OR and the latter is a logical OR.

Isn't it also a logical OR without the short circuit evaluation?

#include<stdio.h>

int first()
{
    printf("\nIn first function");
    return 1;
}

int second()
{
    printf("\nIn second function");
    return 0;
}

int main(void)
{
    if(first() | second())
    {
        printf("\nWithout short circuit evaluation");
    }
    putchar('\n');
    if(first() || second())
    {
        printf("\nWith short circuit evalutaion");
    }
    getchar();
    return 0;
}

>Can u please Correct it I have tried but cant do it
Now I don't understand. I gave you the corrected function. What more do you want?

>Isn't it also a logical OR without the short circuit evaluation?
It can be used as such, provided you're careful. It's generally a bad idea to use the bitwise operators for boolean logic. There can be surprising results.

I have made some changes But still it does not generate the required results it printf only one number not the series

#include<stdio.h>
int fab (int num);
int main (void)
{
int num;
clrscr();
printf("Please enter the no yo which You want Fibonacci series");
scanf("%d",&num);
printf("%d",fab(num));
return(0);
}
int fab (int num)
{
if(num==0 || num==1)
return num;
else
return fab(num-1)+fab(num-2);
}

>But still it does not generate the required results it printf only one number not the series
That's because you only ask for one number. fab gives you the ith number in the series, not the entire series. If you want the entire series, you need to call fab in a loop:

int i;

for ( i = 0; i < 10; i++ )
  printf ( "%d ", fab ( i ) );
printf ( "\n" );

And you need to start formatting your code. Also, clrscr is declared in conio.h. Your code shouldn't compile until you remove it.

Thnx Dear

U really helped me Thnx again

NOw i just have to Understand it correctly Again LOlz

Can u plzz Suggest a book for begginers in C

The best beginner's book I've found for C is "Pointers on C" by Kenneth Reek.

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.