[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 76 KB, 590x568, 1320355949416.jpg [View same] [iqdb] [saucenao] [google]
4071237 No.4071237 [Reply] [Original]

Well I'm probably gonna mess this shit all over so forgive me if you think you're reading something a monkey typed.

I'm learning how to program functions on C, using Dev C++, I was doing my first exercise (to make a function that does the same that the function pow(x,y) does) and after trying to compile this code:

main()
{
float base, pot();
int exponent;
printf("Type base and exponent");
scanf("%f %d",&base,&exponent);
printf("\nThe power is %f\n",pot(base,exponent));
system("PAUSE");
return 0;
}

float pot(a,n)
{
float a;
int n;
if (n<1)
return(a*pot(a,n-1));}

I get these error messages from the compiler:
In function pot:
a redeclared as different kind of symbol
previous definition of a was here.
And the same for n.

Can someone tell me what does this mean, why does it happen and how to fix it? Also if you could tell me if my recursive approach is right (which I doubt) I would thank you a lot.

>2011
>C
Pic related

>> No.4071244

>>4071237
it should be

float pot(float a, int n)

and, you're declaring them twice (once in function arguments and then in the function itself), which is illegal

also, learn indentation for christs sake

>> No.4071248

I don't know C... what's going on in

>float base, pot();

>> No.4071251

>>4071244
can't indent in a text box, bro, just try it

>> No.4071254

>pot

>> No.4071258

>float base, pot();

That's horrific code.

By symbols it just means the identifiers you use for variables, functions etc.

>> No.4071260

>>4071258
what is he doing in that line

>> No.4071263

>>4071251
if (op == GL_FAGGOT)
    for ( ; ; )
        printf("Op is a huge faggot");

>> No.4071266

>>4071263
>he uses spaces to indent

>> No.4071269

>>4071244
I changed what you said and now I've got a new error message:

conflicting types for pot
an argument type that has a default promotion can't match an empty parameter name list declaration.

I lol'd when I realized that you were thinking of "pot" as a pot, well I named it that way because I'm from an spanish speaking country (hence my poor english).

What do I do now?

>> No.4071270

>float base, pot();
>scanf
>system("PAUSE");

>float pot(a,n)
>{
>float a;
>int n;

Anyway, the last one's the one giving you the error.

>> No.4071280

>>4071270
you don't need to declare a and n in the function dawg

>> No.4071282

>>4071269
here's how the internet can help you solve a problem:
1. make a test case that has the least possible code that reproduces the bug
2. copy EVERYTHING the compiler tells you (like, line numbers!)
3. give us the source in a proper format (use pastebin etc)

>> No.4071294

>>4071282
well that last error is in the function pot, look what I typed first, look what the dude told me to change, there is the shit
Line 16 but that isn't necessary in this case I believe.

>> No.4071299

main()
{
float base, pot();
>> why the fuck are you declaring pot() like it's a variable?

int exponent;
printf("Type base and exponent");
scanf("%f %d",&base,&exponent);
printf("\nThe power is %f\n",pot(base,exponent));
system("PAUSE");
return 0;
}

float pot(a,n)
{
>>this should be declared before main(). You need to declare it as float pot(float a, intn)
float a;
int n;
>>these don't belong here.

if (n<1)
return(a*pot(a,n-1));}
>> In the event that n>=1 your function won't return anything. This is illegal.

>> No.4071312

>>4071299
As far as I know, one has to write something known as the prototype of a function whenever its going to be used in another function and its not int type.
Maybe the fucking book I'm using is too old, I could relate the next thing you say with this.
Okay, I thought I had screwed things up with this, how could I do it instead?

>> No.4071315

>>4071312
you must declare a prototype, yes, but on a global scope (above the function declaration) - because everything is procedural, these things count

>> No.4071321

>>4071315 you must declare a prototype
Or just write your functions in an order that makes sense.

>> No.4071325

>>4071321
The proper way is to make a header file and put all the prototypes there.

>> No.4071326

ITT: Why pot smokers shouldn't code.

>> No.4071332

>>4071326
Don't smoke pot(); kids

>> No.4071336 [DELETED] 
File: 87 KB, 469x428, Trollface..jpg [View same] [iqdb] [saucenao] [google]
4071336

>>4071326
>programming experience: over 10 yeas
>working as a programmer
>studying CS and getting A's to good B's
>smoking daily
youmad?

>> No.4071359

>>4071237
Step 1 - get a decent compiler. IIRC, dev hasn't been updated in forever. Get one of the visual studios express versions 2008 or 2010, and/or gcc.

>> No.4071364

Thanks everybody for your help and also for your sarcasms related to the name of my function.

>> No.4071393
File: 45 KB, 387x600, 1305407954282.jpg [View same] [iqdb] [saucenao] [google]
4071393

Dev C++? what the hell is that.
Get a simple virtual machine and use a linux distro ffs
Get a text editor you fancy, gcc for compile, gdb to debug. If you intend to do serious programming install Valgrind to check Memory leaks, and DDD for debugging you code when it gives errors it really helps.

As a note don't forget to declare your functions before the main or if you declare them after put the funtion's header before the main so it is declared.

>> No.4071400

> 2011
> not using TCO