Problem getting a function to compile

 
int A=3;
int B=4;
int C=Gipo(A,B);
int Gipo(int a,int b)
{
int c = (A+B) ;
return(c);

}

what is wrong with this function ? I can not work it out, I copied it exactly from a function written on the internet ? ? ?

 

Too many c's

 
Ickyrus:

Too many c's


sorry I dont know what you mean, I am just learning programming
 

you really should read the book.

without the full program code there are things missing in what you are compiling to give full understanding of what is going wrong.

To understand the problem you need to understand the 'scope' of a variable - that is where it is valid and understood in the code and where you are redefining it.

I think the problem you are having is that the first int c becomes a globally defined variable so when you again define it within a function the compiler rejects the code as it can not create two valid memory references with the same name, the name in this case being 'c'.

 
Ickyrus:

you really should read the book.

without the full program code there are things missing in what you are compiling to give full understanding of what is going wrong.

To understand the problem you need to understand the 'scope' of a variable - that is where it is valid and understood in the code and where you are redefining it.

I think the problem you are having is that the first int c becomes a globally defined variable so when you again define it within a function the compiler rejects the code as it can not create two valid memory references with the same name, the name in this case being 'c'.


I am reading the book. As I said I am just learning but thanks for that explanation it does make sense, and that is the only "c" I have in the program

 

The reserved word 'int' declares, that is reserves memory space, and for human convieniece a compiler attaches a sequence of 'letter(s)' (or a variable name) to memory location reserved and to keep track of things creates a variable names table with assigned memory location. When compiling functions, take note I say functions not programs, as a whole program is basically a number of functions, the varables in a function are assigned memory at the begining of a function call and released from assignment at the end of a function call (keeping it simple though assignment and release of memory can be done at any time) thus when you pass values to a function unless specifically stated otherwise your main program variables do not change value in a function call. This feature alows within a function the same variable name to be used more than once IN DIFFERENT FUNCTIONS providing that the name has not been declared previously. Visually, lets say, you have a house with rooms and in each room you have a human called Dave, while each Dave remains in their assigned room you have no problem in identifying them but if you walk in to a room accompanied by another human called 'Dave', that is a 'Dave' available to you in all the rooms (a global 'Dave') you have problems identifing which 'Dave' you wish to talk to.

 

It seems to me that you have been spoilt by VB, where case is not important. i.e. variable b is not same as variable B.

Also, please use SRC button

So, change

int A=3;
int B=4;
int C=Gipo(A,B);
int Gipo(int a,int b)
{
int c = (A+B) ;
return(c);
}

to

int A=3;
int B=4;
int C=Gipo(A,B);
int Gipo(int a,int b)
{
int c = (a+b) ; // NOTE a+b & not A+B
return(c);
}

NB just typed, not compiled or checked

 

Thanks brewmanz - I tend to forget case sensitivity, used too many languages over my too many years.

 

Still won't compile.

If this is outside of a function, then the int C=Gipo() won't compile, non-constant initialization.

If this is inside of a function, then the function definition is unexpected. Put the definition outside.

 

I tested this and it compiles

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   int A=3;
   int B=4;
   int C=Gipo(A,B);
   return(0);
  } 
  int Gipo(int a,int b)
  {
   int c = (a+b) ; // NOTE a+b & not A+B
   return(c);
  } 
The compiler for MT4 does not like to define functions within a function so keep it simple and define each function separately. Some languages do alow functions to have their own private functions and maybe someone else on the forum will tell us how its done :-)
Reason: