obad62
asked on
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();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Add void fibonacci(int num); before main function
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
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
ASKER
thank you this the old message and it is still dispaly :
source file not compiled
however , I did compile before run for both
!?!?!
source file not compiled
however , I did compile before run for both
!?!?!
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();
}
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();
}
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);
}