# Auto load arguments for function from array

**URL:** <https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554>\
**Category:** Programming\
**Created:** [January 31, 2022, 8:35am UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554 "2022-01-31T08:35:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ardubit](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardubit/32/264049_2.png) [@Ardubit](https://forum.arduino.cc/u/Ardubit)\
**Post date:** [January 31, 2022, 8:35am UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554/1 "2022-01-31T08:35:24Z")

</div>

I have function that takes 5 arguments.

I have a large array that holds the arguments in sets of 5. Like this - I have just shown for 3 sets

```cpp
float param[100] = { 2,0.5,4.5,0.0,5.0
                                     2,4.5,0.5,0.0,5.0
                                     3,2.5,2.5,0.0,5.0 };

```

So the function inside the loop() will use the first set of arguments , complete something and then will need to fetch the next set of 5 arguments and work on that, till the complete set of arguments are used up.

Will be happy to get some ideas to do this.

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [January 31, 2022, 8:36am UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554/2 "2022-01-31T08:36:49Z")

</div>

Why not use a two dimensional array?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 31, 2022, 8:49am UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554/3 "2022-01-31T08:49:27Z")

</div>

> [@Ardubit](#):
>
> arguments in sets of 5. Like this

Not quite like that I hope. You have commas missing

I agree with AWOL. Use a 2 dimensional array, or just pretend that it is a 2 dimensional array and do this

```arduino
float param[100] = {2, 0.5, 4.5, 0.0, 5.0,
                    2, 4.5, 0.5, 0.0, 5.0,
                    3, 2.5, 2.5, 0.0, 5.0
                   };

void setup()
{
  Serial.begin(115200);
  for (int set = 0; set < 3; set++)
  {
    for (int member = 0; member < 5; member++)
    {
      Serial.print(param[set * 5 + member]);
      Serial.print("\t");
    }
    Serial.println();
  }
}

void loop()
{
}
```

---

<div class="post-metadata">

**Author:** ![Ardubit](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardubit/32/264049_2.png) [@Ardubit](https://forum.arduino.cc/u/Ardubit)\
**Post date:** [January 31, 2022, 9:27am UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554/4 "2022-01-31T09:27:07Z")

</div>

Honestly never thought of that ! Let me try and work on it !! Thanks

---

<div class="post-metadata">

**Author:** ![Ardubit](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ardubit/32/264049_2.png) [@Ardubit](https://forum.arduino.cc/u/Ardubit)\
**Post date:** [January 31, 2022, 9:30am UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554/5 "2022-01-31T09:30:07Z")

</div>

Thanks ... yes this is possible when i know beforehand the number of data sets. Maybe I need to tweak this to exit the loop upon meeting a particular end marker character ! Maybe a float that is way out of the expected data range .

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 31, 2022, 3:01pm UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554/6 "2022-01-31T15:01:32Z")

</div>

> [@Ardubit](#):
>
> this is possible when i know beforehand the number of data sets.

There is no way to make an array of constants WITHOUT knowing the number of data sets.

```arduino
float param[][5] =
{
  {2, 0.5, 4.5, 0.0, 5.0},
  {2, 4.5, 0.5, 0.0, 5.0},
  {3, 2.5, 2.5, 0.0, 5.0}
};
size_t paramCount = sizeof param / sizeof param[0];

void setup()
{
  Serial.begin(115200);
  for (size_t set = 0; set < paramCount; set++)
  {
    for (size_t member = 0; member < 5; member++)
    {
      Serial.print(param[set][member]);
      Serial.print("\t");
    }
    Serial.println();
  }
}

void loop()
{
}
```

---

<div class="post-metadata">

**Author:** ![cedarlakeinstruments](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cedarlakeinstruments/32/1128931_2.png) [@cedarlakeinstruments](https://forum.arduino.cc/u/cedarlakeinstruments)\
**Post date:** [January 31, 2022, 6:33pm UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554/7 "2022-01-31T18:33:39Z")

</div>

If I find myself passing five arguments to a function, I start thinking of ways to reduce that number. It's far too easy in a language like C++ that doesn't have named arguments to accidentally swap arguments.

Consider passing a struct or a class as a parameter and using a array of structs/classes instead.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 30, 2022, 6:34pm UTC](https://forum.arduino.cc/t/auto-load-arguments-for-function-from-array/953554/8 "2022-07-30T18:34:10Z")

</div>

Closed on July 30, 2022, 6:34pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
