# Understand building function

**URL:** <https://forum.arduino.cc/t/understand-building-function/1037063>\
**Category:** General Guidance\
**Created:** [September 30, 2022, 11:22am UTC](https://forum.arduino.cc/t/understand-building-function/1037063 "2022-09-30T11:22:03Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jadbara](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jadbara](https://forum.arduino.cc/u/jadbara)\
**Post date:** [September 30, 2022, 11:22am UTC](https://forum.arduino.cc/t/understand-building-function/1037063/1 "2022-09-30T11:22:03Z")

</div>

good day everyone  
I'm trying to build this function that set LCD cursor and print some text

```cpp
void setAndprint(byte column, byte row, const char msg[]) {
  lcd.setCursor(column, row);
  lcd.print(msg);
}

```

the target is to reduce program space because I use these two lines alot in my program and it really works nice, but the problem is that I don't want to use this function only with chars (because I declared `const char msg[]`), I want to print `bool`s or `int`s. and my function in this case can print only chars, how to make it general, just like `lcd.print()`

must I write a seperate function for each type ? or there is some magic can I do

thank you

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 30, 2022, 11:36am UTC](https://forum.arduino.cc/t/understand-building-function/1037063/2 "2022-09-30T11:36:10Z")

</div>

> [@jadbara](#):
>
> must I write a seperate function for each type ? or there is some magic can I do

yes you need one function per different type, using overloading  
this can also be achieved by template, something like

```arduino
template<typename T> 
void setAndprint(const byte x, const byte y, T data ) { 
  lcd.setCursor(x, y);
  lcd.print(data);
}

```

(typed here so untested)

then it would work for anything that print() supports

---

<div class="post-metadata">

**Author:** ![jadbara](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jadbara](https://forum.arduino.cc/u/jadbara)\
**Post date:** [September 30, 2022, 11:43am UTC](https://forum.arduino.cc/t/understand-building-function/1037063/3 "2022-09-30T11:43:50Z")

</div>

but there is already a `print` function written for each type, I think there should be a way to not write it again

---

<div class="post-metadata">

**Author:** ![jadbara](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jadbara](https://forum.arduino.cc/u/jadbara)\
**Post date:** [September 30, 2022, 11:48am UTC](https://forum.arduino.cc/t/understand-building-function/1037063/4 "2022-09-30T11:48:41Z")

</div>

wow! that's the magic I was talking about  
thank you

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 30, 2022, 12:50pm UTC](https://forum.arduino.cc/t/understand-building-function/1037063/5 "2022-09-30T12:50:43Z")

</div>

Otherwise use a macro - it will auto adapt as well

---

<div class="post-metadata">

**Author:** ![jadbara](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jadbara](https://forum.arduino.cc/u/jadbara)\
**Post date:** [September 30, 2022, 5:29pm UTC](https://forum.arduino.cc/t/understand-building-function/1037063/6 "2022-09-30T17:29:29Z")

</div>

an example please

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 30, 2022, 8:44pm UTC](https://forum.arduino.cc/t/understand-building-function/1037063/7 "2022-09-30T20:44:29Z")

</div>

Something like  
`#define setAndprint(x, y, data ) { lcd.setCursor((x), (y)); lcd.print((data));}`

---

<div class="post-metadata">

**Author:** ![jadbara](https://avatars.discourse-cdn.com/v4/letter/j/bcef8e/32.png) [@jadbara](https://forum.arduino.cc/u/jadbara)\
**Post date:** [September 30, 2022, 8:51pm UTC](https://forum.arduino.cc/t/understand-building-function/1037063/8 "2022-09-30T20:51:24Z")

</div>

that's wrong now, defining a function using `#define` makes the compiler replacing the function literally with its code, the size of code will still huge, so we didn't solve the problem, but we converts it to another shape

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 30, 2022, 9:00pm UTC](https://forum.arduino.cc/t/understand-building-function/1037063/9 "2022-09-30T21:00:17Z")

</div>

Yes that’s why I did not propose this at first. It’s not a function call for sure (but just two so that won’t be as bad as you make it sound…)

---

<div class="post-metadata">

**Author:** ![killzone\_kid](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/killzone_kid/32/1098074_2.png) [@killzone\_kid](https://forum.arduino.cc/u/killzone_kid)\
**Post date:** [September 30, 2022, 9:40pm UTC](https://forum.arduino.cc/t/understand-building-function/1037063/10 "2022-09-30T21:40:51Z")

</div>

with template the compiler will create as many functions as different types you try to pass to it

---

<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:** [March 29, 2023, 9:41pm UTC](https://forum.arduino.cc/t/understand-building-function/1037063/11 "2023-03-29T21:41:47Z")

</div>

Closed on March 29, 2023, 9:41pm UTC

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