I am using a DSP and am trying to be conservative on memory. I have this fairly big code block which I need to use 2 times within main program (the stuff within the if block):
if(calbp<8){
probecals=EEaddP[3];
usercals=EEaddP[4];
cboa=calbp*0x100+0x100; //block pointer
//Grab previous calibration block
EEaddP=setadd13_19(0x90010000+cboa); //Set pointer to Calibration settings block
lrcr=EEaddP[cboa+1];
hrcr=EEaddP[cboa+2];
for (lus1=3;lus1<8;lus1++)
measlo[lus1-3]=(float)EEaddP[cboa+lus1];//get lo measurements
for (lus1=8;lus1<13;lus1++)
meashi[lus1-8]=(float)EEaddP[cboa+lus1];//get high measurements
for (lus1=13;lus1<18;lus1++)
callo[lus1-13]=(int)EEaddP[cboa+lus1]; //get lo rnage calibration numbers
for (lus1=18;lus1<23;lus1++)
calhi[lus1-18]=(int)EEaddP[cboa+lus1]; //store hi range calibration numbers
lus1=EEaddP[cboa+23]; //get high word of ref comp
lus2=EEaddP[cboa+24]; //get low word of ref comp
li=lus2+lus1*10000; //recompose refcomp
refcomplo=(float)li/100000;
lus1=EEaddP[cboa+25]; //get high word of ref comp
lus2=EEaddP[cboa+26]; //get low word of ref comp
li=lus2+lus1*10000; //recompose refcomp
refcomphi=(float)li/100000;
for (y=0;y<lrcr;y++){ //calc slopes and intercepts for low range based on EEPROM data
slopelo[y]=(float)(callo[y]*10-callo[y+1]*10)/(measlo[y]-measlo[y+1]); //Calc slope
interceptlo[y]=(float)(callo[y]*10)-(measlo[y])*slopelo[y]; //Calc intercept
}
for (y=0;y<hrcr;y++){ //calc slopes and intercepts for high range based on EEPROM data
slopehi[y]=(float)(calhi[y]-calhi[y+1])/(meashi[y]-meashi[y+1]); //Calc slope
intercepthi[y]=(float)(calhi[y])-(meashi[y])*slopehi[y]; //Calc intercept
}
calibrated=1; //set calibrated flag good
}
My issue is if I use a function/subroutine, I have so many variables to extract out of it which makes it seem pointless. What is the most effective way. I was thinking about separating this block of code and using the evil goto in this case. Any suggestions. Thanks in advance.
If you have many variable to extract you could use a class/struct to hold the variables. Alternatively use a loop, since you have to run it twice. Whatever you do DO NOT USE GOTO.
in reply to Spaggy:
I would have to run it in 2 different areas of the program one at the very beginning for initialization and one if they want to recall a different block of data through a menu sequence, so a loop option is not an alternative.
In reply to Helios
regarding the Goto, I have never used one yet. although, I am a beginning programmer, I have been able to stay away from them so far.
I do see examples, and see a return at the end of the goto label loop, will that return you to the place that the goto was executed?
Could I make all the variables needed in a global kind of mode so that they will be updated within the function call and be available for the main. I have done this already and am familiar with it. I have never worked with structures yet and feel a little hesitant about trying to invoke one.
Are all of the variables that you want to use of the same type, if they are you could pass a pointer to that type to a function and the function can write the data into the memory allocated for that pointer. Alternatively, you could create a namespace to put the global variables in, but don't do usingnamespace yourNamespaceName.
I do see examples, and see a return at the end of the goto label loop, will that return you to the place that the goto was executed?
No. In older languages such as BASIC, there was a command called gosub which had the same behavior as goto, with the exception that it would return control to the statement that followed the gosub when it found a return statement.
You definitely need a function. There are few cases when using global data is a good idea, but I don't think this is one of them. Judging by what you've told us, I think the best way to organize this would be to put the data in a class and have member functions modify its contents.
Yeah, sure, a struct works, too. Since classes aren't supported, member functions are probably not supported, either, so you'll have to use normal functions and pass a pointer to the struct.