Choose Language Hide Translation Bar
Highlighted
TheRealYeti
Level II

Is it possible to have interchangeable arguments in a function?

For example I have function like below:

 

my_function = Function( 
    {
        M_DATA      = Empty(),
        M_AGE        = Empty()
    }, {},
);
    

 

 

is there a way I can call the function like this

my_function( M_AGE = 12, M_DATA = "Useful info");

 

Age and Data were interchanged but function would capture it correctly?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Georg
Level IV

Re: Is it possible to have interchangeable arguments in a function?

I don't think so, JMP captures the Arguments in the defined order.

If you would like to be Independent on the order, you could use a more complex data structure as Argument, like associative Array.

See example. BR

 

// function with single arguments
my_function = Function( {M_DATA = Empty(), M_AGE = Empty()},
	{},
	Show( M_DATA, M_AGE )
);

Print( "swapped arguments are ignored" );
my_function( M_AGE = 12, M_DATA = "Hello" );


// function with associative array AA
my_function_AA = Function( {Local_AA = Empty()},
	{},
	keys = Local_AA << get keys;
	For( i = 1, i <= N Items( keys ), i++,
		Show( keys[i], Local_AA[keys[i]] )
	);
);

AA = Associative Array( {{"M_AGE", 12}, {"M_DATA", "Hello"}} );
my_function_AA( AA );
Georg

View solution in original post

2 REPLIES 2
Highlighted
Georg
Level IV

Re: Is it possible to have interchangeable arguments in a function?

I don't think so, JMP captures the Arguments in the defined order.

If you would like to be Independent on the order, you could use a more complex data structure as Argument, like associative Array.

See example. BR

 

// function with single arguments
my_function = Function( {M_DATA = Empty(), M_AGE = Empty()},
	{},
	Show( M_DATA, M_AGE )
);

Print( "swapped arguments are ignored" );
my_function( M_AGE = 12, M_DATA = "Hello" );


// function with associative array AA
my_function_AA = Function( {Local_AA = Empty()},
	{},
	keys = Local_AA << get keys;
	For( i = 1, i <= N Items( keys ), i++,
		Show( keys[i], Local_AA[keys[i]] )
	);
);

AA = Associative Array( {{"M_AGE", 12}, {"M_DATA", "Hello"}} );
my_function_AA( AA );
Georg

View solution in original post

Highlighted
TheRealYeti
Level II

Re: Is it possible to have interchangeable arguments in a function?

Thanks @Georg  for this work around! It is not very hard to implement.

Article Labels

    There are no labels assigned to this post.