0
votes

Gcc gives the error "functions.c: In function 'retryop': functions.c:109:3: warning: implicit declaration of function 'main' [-Wimplicit-function-declaration] main(); ^~~~"

I'm creating a function that calls back to main to restart the file, but i don't know how to have it return since main is declared after my functions file. Any help with this would be very greatly appreciated.

Main.c

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <time.h>
#include "functions.c"

int main()
{
setup();

char type;
double number1;
double number2;
int temp;
bool retry;

printf("Please choose what operation you would like to do  ( +, -, *,/, or R): \n");
scanf("%c", &type);

if (type == '+')
{
    setup();

    printf("Addition\n");
    printf("\n");

    printf("Please enter Addend 1:");
    scanf("%lf", &number1);

    printf("Please enter Addend 2:");
    scanf("%lf", &number2);

    double answer = number1 + number2;

    printf("The Sum is: %f", answer);

    retryop();
}
else if (type == '-')
{
    setup();

    printf("Subtraction\n");
    printf("\n");

    printf("Please enter Your Minuend:");
    scanf("%lf", &number1);

    printf("Please enter Your Subtrahend:");
    scanf("%lf", &number2);

    double answer = number1 - number2;

    printf("The Differnce is: %f", answer);

    retryop();
}
else if (type == '*')
{
    setup();

    printf("Muliplication\n");
    printf("\n");
    
    printf("Please enter Factor 1:");
    scanf("%lf", &number1);

    printf("Please enter Factor 2:");
    scanf("%lf", &number2);

    double answer = number1 * number2;

    printf("Your Product is: %f", answer);

    retryop();
}
else if (type == '/')
{
    setup();

    printf("Division\n");
    printf("\n");

    printf("Please enter your divadend:");
    scanf("%lf", &number1);

    printf("Please enter your divisor:");
    scanf("%lf", &number2);

    double answer = number1 / number2;

    printf("Your quotient is: %f", answer);

    retryop();
}
else if (type == 'R')
{
    setup();

    int maxValue;
    srand(time(NULL));
    printf("Please enter your max value:");
    scanf("%i", &maxValue);
    int random = (rand() % (maxValue + 1));
    printf("Here is your random number: %i", random);

    retryop();
}
else if (type == 'r')
{
    setup();

    int maxValue;
    srand(time(NULL));
    printf("Please enter your max value:");
    scanf("%i", &maxValue);
    int random = (rand() % (maxValue + 1));
    printf("Here is your random number: %i\n", random);

    retryop();
}
else
{
    setup();

    printf("Please Re-run your program and enter a valid Operator\n");
    system("pause");
    Main();
}
}

Functions.c

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

char type;
double number1;
double number2;
double answer;
bool retry;
char temp;

void setup()
{
system("cls");
}

int typeselect()
{
printf("Please choose what operation you would like to do  ( +, -, *,/, or R): \n");
scanf("%c", &type);

return type;
}

int retryop()
{
printf("Would you like to do this again? [Y/N]");
scanf("%i", &temp);
if (temp == 'Y')
{
    system("pause");
    Main();
}
else if (temp == 'y')
{
    system("pause");
    Main();
}

else if(temp == 'N')
{
    system("pause");
    return 0;
}
else if(temp == 'n')
{
    system("pause");
    return 0;
}
else
{
    system("pause");
    return 0;
}
}
1
Don't code everything in the main.Cid
main() is different than Main()Cid
@Cid I fixed that, and it still gives the exact same errorBubba656
Repeat, don't code everything in main. Write seperate function(s) for that. And more generally, functions need to be declared before they are used, see: warning: implicit declaration of functionkaylum
Well, you have not declared main() in functions.cklutt

1 Answers

2
votes

Functions should be declared before they are used, and main is no exception. Old versions of C would declare functions implicitly when they were called, and some compilers still allow this, but it is bad practice.

First, in main.c, define main properly, using int main(void). Avoid using int main(); it is better to explicitly state main is being used with no arguments, if that is how you are using it. (You can also use int main(int argc, char *argv[]) if you are using the arguments passed to it.)

Then, in Functions.c, declare main the same way: int main(void);. You can put this line directly in Functions.c or put it in a header file such as main.h, which should be included with an #include statement in both main.c and Functions.c.

That said, having retryop call main the way you do is bad design. The code for retrying should be designed as a loop that repeats if a retry is necessary, not as recursive calls to main.