# Programming difficulty about function definition

**URL:** <https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610>\
**Category:** Programming\
**Created:** [January 5, 2023, 2:14pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610 "2023-01-05T14:14:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![raejinn96](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/raejinn96/32/706451_2.png) [@raejinn96](https://forum.arduino.cc/u/raejinn96)\
**Post date:** [January 5, 2023, 2:14pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/1 "2023-01-05T14:14:26Z")

</div>

I was practicing and trying my Analog to Digital Converter (MCP3008) and trying this simple test. And here is my code :

```cpp
#include <Adafruit_MCP3008.h>

Adafruit_MCP3008 adc;

  float voltage(int raw){
  return raw / 1023 * 4.9;
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("MCP3008 simple test.");

  adc.begin();
  
}

void loop() {
  int raw = adc.readADC(0);

  Serial.print(raw);
  Serial.print("\t");
  Serial.println(voltage(raw));

  delay(1000);
}

```

and when I opened my serial monitor it only showing the changes of "raw", but my "voltage" function seems not get included, here is what I got, as you can see it only shows the result for raw (on left), but not showing the voltage (on right)  
 ![Screenshot (62)](https://europe1.discourse-cdn.com/arduino/original/4X/2/6/4/264b73c72fe43cb56f8678f9006b532ac9ccf470.png)

I'm trying to make sure I got the "voltage" function affected also inside the void loop. can anybody explain me the rule of program in this case ?

---

<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 5, 2023, 2:20pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/2 "2023-01-05T14:20:29Z")

</div>

Try this

```cpp
float voltage(int raw)
{
  return raw / (1023 * 4.9);
}

```

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [January 5, 2023, 2:20pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/3 "2023-01-05T14:20:40Z")

</div>

> [@raejinn96](#):
>
> `return raw / 1023 * 4.9;`

`return raw / 1024.0 * 4.9;`

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [January 5, 2023, 2:21pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/4 "2023-01-05T14:21:08Z")

</div>

Hi,  
Do the maths.

119/1023 = 0.116 so as an integer the result is 0.  
so 0 \* 4.9 = 0

You need to look at using float rather than int variables.

Tom.. 😃 👍 ☕ 🇦🇺  
PS. Do the multiplication before the division.

---

<div class="post-metadata">

**Author:** ![StefanL38](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/stefanl38/32/713548_2.png) [@StefanL38](https://forum.arduino.cc/u/StefanL38)\
**Post date:** [January 5, 2023, 2:25pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/5 "2023-01-05T14:25:57Z")

</div>

So with an int as parameter

```cpp
float voltage(int raw){
  // it is important to have the "1.0" DOT-ZERO
  // to make the compiler handle it explicit as float
  float myFLoat = raw * 1.0; 
  myFLoat = myFLoat / 1023 * 4.9;
  return myFLoat;
}

```

or simply

```cpp
float voltage(float raw){
  return raw / 1023 * 4.9;
}

```

---

<div class="post-metadata">

**Author:** ![raejinn96](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/raejinn96/32/706451_2.png) [@raejinn96](https://forum.arduino.cc/u/raejinn96)\
**Post date:** [January 5, 2023, 2:29pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/6 "2023-01-05T14:29:03Z")

</div>

OMG thanks for the clear info, so the problem was the mathematical order damn....  
I tried this and it solved

```cpp
float voltage(int raw)
{
  return (raw * 4.9) / 1023 ;
}

```

thank you for noticing the problem guys

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [January 5, 2023, 2:30pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/7 "2023-01-05T14:30:12Z")

</div>

> [@StefanL38](#):
>
> or simply
> 
> ```arduino
> float voltage(float raw){
> return raw / 1023 * 4.9;
> }
> 
> ```

Or, correctly, divide by 1024.  
It's a small thing, but small things can be important (unlike Andrew Tate's you-know-what)

---

<div class="post-metadata">

**Author:** ![raejinn96](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/raejinn96/32/706451_2.png) [@raejinn96](https://forum.arduino.cc/u/raejinn96)\
**Post date:** [January 5, 2023, 2:30pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/8 "2023-01-05T14:30:17Z")

</div>

yup yup silly me, now my problem fixed by doing this :

```cpp
float voltage(int raw)
{
  return (raw * 4.9) / 1023 ;
}

```

thank you

---

<div class="post-metadata">

**Author:** ![raejinn96](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/raejinn96/32/706451_2.png) [@raejinn96](https://forum.arduino.cc/u/raejinn96)\
**Post date:** [January 5, 2023, 2:32pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/9 "2023-01-05T14:32:25Z")

</div>

yup thank you for noticing first, I did this instead :

```cpp
float voltage(int raw)
{
  return (raw * 4.9) / 1023 ;
}

```

and it fixed my program issue, turn out it was not the function definition, it was mathematical order, silly me 😅

---

<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 4, 2023, 2:33pm UTC](https://forum.arduino.cc/t/programming-difficulty-about-function-definition/1073610/10 "2023-07-04T14:33:25Z")

</div>

Closed on July 4, 2023, 2:33pm UTC

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