pass to another function

I have this main function calls another function but it dosn't work

#include <stdio.h> 
int main() 
{ 
    int num = 0;    
    printf("Entering the value "); 
    scanf("%d", &num); 
    fibonacci(num); 
    return(0);
} 


void fibonacci(int num) 
{ 
    int a = 0, b = 1; 
    int count = 0; 
    printf("0 1 "); 
    do { 
        b = a + b; 
        a = b - a; 
        b = a + b - a; 
        printf("%d ", b); 
        count++; 
    } while 
      (count <= num - 3); 
    printf("\n");
    getch();  
}

Open in new window

obad62Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
define doesn't work?


you need to prototype the function, see code below.

#include <stdio.h> 

void fibonacci(int num);

int main() 
{ 
    int num = 0;    
    printf("Entering the value "); 
    scanf("%d", &num); 
    fibonacci(num); 
    return(0);
} 


void fibonacci(int num) 
{ 
    int a = 0, b = 1, c; 
    int count = 0; 
    printf("0 1 "); 
    do { 
       //get total
        c = a + b;
      //advance numbers
        a = b;  
        b = c; 
      // no need for this line  b = a + b - a; 
       //  b = (a - a) + b
       //  b = 0 + b
       // b  = b
        printf("%d ", b); 
        count++; 
    } while 
      (count <= num - 3); 
    printf("\n");
    getch();  
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pgnatyukCommented:
Add void fibonacci(int num); before main function
0
w00teCommented:
In old style c the main function would come last -- you would have put the whole fibbonacci code block before main and then main would know what it was becasue it was defined first.
In newer versions of C and in C++ you use prototypes as these 2 suggested.  Putting
void fibonacci(int num);
before main and defining fibbonacci after main lets the compiler know to look lower in the file for the definition when it encounters  the prototype at the top.  That way when the call is made in main() the program knows what the function is because it ran into it and compiled it when the prototype was found.
-w00te
0
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

obad62Author Commented:
thank you this the old message and it is still dispaly :

source file not compiled

however , I did compile before run for both


!?!?!
0
pgnatyukCommented:
I attached the code that's compiled well.
#include <stdio.h> 
void fibonacci(int num);

int main() 
{ 
    int num = 0;    
    printf("Entering the value "); 
    scanf("%d", &num); 
    fibonacci(num); 
    return(0);
} 


void fibonacci(int num) 
{ 
    int a = 0, b = 1; 
    int count = 0; 
    printf("0 1 "); 
    do { 
        b = a + b; 
        a = b - a; 
        b = a + b - a; 
        printf("%d ", b); 
        count++; 
    } while 
		(count <= num - 3); 
    printf("\n");
    getchar();  
}

Open in new window

0
w00teCommented:
Use the code below, compile with g++ filename.cpp, run with ./a.out in the same directory on unix/linux.  It works.  Visual c++ should also let you one-click run with visual studio.
#include <stdio.h> 

//Prototype 
void fibonacci(int num);

int main()  
{  
    int num = 0;     
    printf("Entering the value ");  
    scanf("%d", &num);  
    fibonacci(num);  
    return(0); 
}  
 
void fibonacci(int num)  
{  
    int a = 0, b = 1;  
    int count = 0;  
    printf("0 1 ");  
    do {  
        b = a + b;  
        a = b - a;  
        b = a + b - a;  
        printf("%d ", b);  
        count++;  
    } while  
      (count <= num - 3);  
    printf("\n"); 
    getch();   
}

Open in new window

0
Kyle AbrahamsSenior .Net DeveloperCommented:
If you are using the older style version try my code below.
#include <stdio.h> 

void fibonacci(int num) 
{ 
    int a = 0, b = 1, c; 
    int count = 0; 
    printf("0 1 "); 
    do { 
       //get total
        c = a + b;
      //advance numbers
        a = b;  
        b = c; 
      // no need for this line  b = a + b - a; 
       //  b = (a - a) + b
       //  b = 0 + b
       // b  = b
        printf("%d ", b); 
        count++; 
    } while 
      (count <= num - 3); 
    printf("\n");
    getch();  
}

int main() 
{ 
    int num = 0;    
    printf("Entering the value "); 
    scanf("%d", &num); 
    fibonacci(num); 
    return(0);
} 

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.