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?
 
Kyle AbrahamsConnect With a Mentor Senior .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
 
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.