mardi 14 février 2023

Return variable declared in for loop [duplicate]

int square(int i) // function which calculates the square of any number
{
    for(int t = 0; t <= i; t += i); // t=0 at first, then as long as t is less than or equal to i, increase t by i
    return t;
}

I expected this function to work. However, the compiler said "t is not defined". However, I said int t = 0, so not sure what to do. Thanks

Aucun commentaire:

Enregistrer un commentaire