predine array in function

hey

i have a function thst passes 2 sets of XYs

float calc_hypot_from_multiple_XY(float[], float[]);

is it possible to predefine an array in c++, like you would any other function. ive tried all sorts of syntax but cant get it to work, eg.

float calc_hypot_from_multiple_XY(float[], float[] {0.0} {0.0});

all i want to do is have the choice of sending either, just the first set of XYs or both

cheers

ive tried all sorts of syntax

Have you tried anything using the assignment operator?

‘=’ ??? - yes

this is the error i get

Mag_Plasma_V4.ino:135:65: error: could not convert ‘{0.0}’ from ‘’ to 'float*
Mag_Plasma_V4.ino:In function 'float SBS::calc_hypot_from_multiple_XY(float*, float*)
Mag_Plasma_V4.ino:135:69: error: expected ‘;’ before ‘}’ token
float SBS*:calc_hypot_from_multiple_XY(float, float = {0.0} {0.0})
Mag_Plasma_V4.ino:At global scope
Mag_Plasma_V4.ino:135:70: error: expected unqualified-id before ‘)’ token
float SBS*:calc_hypot_from_multiple_XY(float, float = {0.0} {0.0})
Error compiling project sources

If you want to post any code, you know, anything that may help us help you, please, don't hold back.

I think I see what you're trying to do. You are trying to write a function? You want this function to either take one array or two arrays as arguments?

Every function in C has a 'signature' which is not just the name of the function. It includes the types of the arguments. Serial.print(int anInteger) is a different function to Serial.print(float aFloat) You can actually write different functions that do different things.

Usually you would write one 'real' version of the function and then all the other variations can just call that one with the appropriate default arguments.

float calc_hypot_from_multiple_XY(float X[] , float Y[]) {
  //do something with 2 arrays
}
float calc_hypot_from_multiple_XY(float Y[]) {
  float DefaultX[] = {...};
  calc_hypot_from_multiple_XY(DefaultX, Y);
}

You can also have functions with variable numbers of arguments so if you're only dropping arguments off the right-hand end then you can do it in one function.

float calc_hypot_from_multiple_XY( float firstXYs[], float secondXYs[]) {
	float x;
	float y;
	float h;
	for(int i = 0; i < 2; i++) {
		if(i == 0) {
			x = (firstXYs[i]) - (secondXYs[i]);
		}
		if(i == 1) {
			y = (firstXYs[i]) - (secondXYs[i]);
			
		}
	}
	h = sqrt((x*x) + (y*y));
	return h;
}

thats what im trying to do morgan. in the function second XYs get subtracted from first XYs. because i cant figure out how to initialise a default value for an array in the function ie. {0.0}. the values that get passed for second XY vary as you would expect. Do you know what the correct syntax to set the values to 0.0 for the second array passed to the function is.

ie if no values get passed then float secondXYs = {0.0, 0.0} gets used

float defaults [2];  // crt0 will initialise this to zero
...
...
float calc_hypot_from_multiple_XY( float* firstXYs, float* secondXYs = defaults)
{
  // yadda yadda
}

(This should work in C++, but the Arduino's prototype generation may barf on it - I haven't tried)

i thought you had it there for a minute.
i couldnt get it to work, the function wont let me pass only 1 array. too few arguments.

one of the suggestions, quick tips, pop ups whatever you call them in as7 had 'NULL' as an option so i tried that

called like

result = SBS::calc_hypot_from_multiple_XY(first, NULL);

function definition

float calc_hypot_from_multiple_XY( float firstXYs[], float secondXYs[]) {
blah
}

i thought you had it there for a minute.

Again, I can’t tell what you tried, but

float defaults [2];  // crt0 will initialise this to zero

float foo( float* firstXYs, float* secondXYs = defaults)
{
  return 0.0; // dummy to keep the compiler happy
}
void setup() 
{
  float x = foo (defaults);
  float y = foo (defaults, defaults);
}
void loop() 
{
}

compiles fine.

Thanks AWOL, didnt mean to second guess the guru.

i tried this in a single ino file in AS7 and all good, i guess theres something happening with visual micro, and multiple H.files and cpp files that im not picking up

float defaults [2];  // crt0 will initialise this to zero

float foo( float* firstXYs, float* secondXYs = defaults)
{
		float x, y, h;
		for(int i = 0; i < 2; i++) {
			if(i == 0) {
				x = (firstXYs[i]) - (secondXYs[i]);
			}
			if(i == 1) {
				y = (firstXYs[i]) - (secondXYs[i]);
			}
		}
		h = sqrt((x*x) + (y*y));
		Serial.println(h);
		return h;
}
void setup()
{
	
	  Serial.begin(115200);
	  while(!Serial);
	float fdsfs[] = {50.0, 40.3};
	float hvjvbkjkn[] = {23.8, 40.3};	
	float x = foo (fdsfs);
	float y = foo (fdsfs, hvjvbkjkn);
}
void loop()
{
}

will keep playing

But then the private defaults are swinging in the breeze as a global. That breaks encapsulation. What if two functions point to the same array and one modifies it? What if one needs the default value to be all ones? What if one needs a different length of zeros? What if you can't afford to waste SRAM storage space on a row of zeros?

The default if the second parameter is missing should be the NULL pointer. Then code in the function can see it was given no valid input for that parameter and it can substitute whatever algorithm it needs.

AWOL:

float defaults [2];  // crt0 will initialise this to zero



float calc_hypot_from_multiple_XY( float* firstXYs, float* secondXYs = defaults)
{
 // yadda yadda
}



(This should work in C++, but the Arduino's prototype generation may barf on it - I haven't tried)

It doesn’t barf, it chokes. It does not create the prototype for functions with a default parameter:

#include <Arduino.h>
#line 1 "C:\\Google Drive\\Documents\\Arduino\\sketch_aug16b\\sketch_aug16b.ino"
#line 1 "C:\\Google Drive\\Documents\\Arduino\\sketch_aug16b\\sketch_aug16b.ino"
#line 1 "C:\\Google Drive\\Documents\\Arduino\\sketch_aug16b\\sketch_aug16b.ino"
void setup();
#line 6 "C:\\Google Drive\\Documents\\Arduino\\sketch_aug16b\\sketch_aug16b.ino"
void loop();
#line 1 "C:\\Google Drive\\Documents\\Arduino\\sketch_aug16b\\sketch_aug16b.ino"
void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:
  funkyfunc();
}

void funkyfunc(int five=5)
{
  
}
C:\Google Drive\Documents\Arduino\sketch_aug16b\sketch_aug16b.ino: In function 'void loop()':

sketch_aug16b:8: error: 'funkyfunc' was not declared in this scope

   funkyfunc();

             ^

C:\Google Drive\Documents\Arduino\sketch_aug16b\sketch_aug16b.ino: At global scope:

C:\Google Drive\Documents\Arduino\sketch_aug16b\sketch_aug16b.ino:11:25: warning: unused parameter 'five' [-Wunused-parameter]

 void funkyfunc(int five=5)

                         ^

exit status 1
'funkyfunc' was not declared in this scope

What if you can't afford to waste SRAM storage space on a row of zeros?

i ended going back to how i originally started, with two functions

func1(float) {
calc hypot from 1 set of XYs
return h;
}

func1(float, float) {
float x;
float y;
calc hypot from 1 set of XYs(x, y)
return h;
}

i guess for now there is no way of predefining to zero, and passing just one parameter through function

Cheers for the replies.