简体   繁体   中英

Question about conflicting types for a function

I'm a student of C and I've got a problem with types of my variables in my function. It's a function to calculate the capital in addition with interest rate in a certain span and it says that there is a conflicting type for "capital_a_terme".

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

int main()
{
    float capital_initial, taux_interet_fixe, nb_annee_placement;
    printf("Saisir le capital initial.\n");
    scanf("%f", &capital_initial);
    printf("Saisir le taux d'interet fixe.\n");
    scanf("%f", &taux_interet_fixe);
    printf("Saisir le nombre d'annee de placement.\n");
    scanf("%f", &nb_annee_placement);
    printf("le capital a terme vaut : %f.\n", capital_a_terme(capital_initial, taux_interet_fixe, nb_annee_placement));
}

float capital_a_terme(float capital_initial, float taux_interet_fixe, float nb_annee_placement)
{
    if (nb_annee_placement == 0)
    {
        return capital_initial;
    }
    else
    {
        return (capital_a_terme(capital_initial + capital_initial * taux_interet_fixe / 100, taux_interet_fixe, nb_annee_placement - 1));
    }
}

If the compiler doesn't know a function signature it defaults its return type to int .

As capital_a_terme is being used before the compiler knows what its signature is, it does exactly that, but when it reaches the line where the function is used it notices that the type does not match with what it thought it was, so the compilation fails and the error is issued.

You need to either place a prototype of the function before you use it, in this case, before main :

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

// prototype here, before it's used
float capital_a_terme(float capital_initial, float taux_interet_fixe, float nb_annee_placement);

int main()
{
    float capital_initial, taux_interet_fixe, nb_annee_placement;
    printf("Saisir le capital initial.\n");
    //...

Or move the implementation up, to before you use it, again before main :

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

// Or the whole function here, before it's used
float capital_a_terme(float capital_initial, float taux_interet_fixe, float nb_annee_placement)
{
    if (nb_annee_placement == 0)
    {
        return capital_initial;
    }
    //... 
}

int main()
{
    float capital_initial, taux_interet_fixe, nb_annee_placement;
    printf("Saisir le capital initial.\n");
    //...

you need to declare this function (above main), so main will recognize it.

on the other side you can define this function above main and then it will be recognized by main.

there's a difference between declaration and definition of a function:

every function needs declaration so other part of the program will recognize it.

also, you can declare a function without defining it and you will able to compile it and run it.

but, you can not define function without declare it.

if you define function above the part of the program that actually use it, the definition is also it's declaration.

hope you got it.

2 things:

  1. space out the return statement:
return (capital_a_terme(capital_initial + capital_initial * taux_interet_fixe / 100, taux_interet_fixe, nb_annee_placement-1));
  1. 100 needs to be a float: (this is the only way I could figure out how to do it based off of my very limited understanding of c, though you might be able to replace onehundred with 100f or %f100 , I don't really know tbh)
float onehundred = 100;
return (capital_a_terme(capital_initial + capital_initial * taux_interet_fixe / onehundred, taux_interet_fixe, nb_annee_placement-1));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM