Custom Function

how do i set a custom function, like void scale () or void weight ()

void scale() {

}

void wait() {



}

you do

void scale() {
 // your code goes here
}

your question was not an introductory tutorial so I moved it to a better place.
do yourself a favour and please read How to get the best out of this forum andur post accordingly (including code tags and necessary documentation for your ask).

Arduino function (reference).

More on Arduino functions.

You need to declare it above the setup() function and define it after the loop() function. For example:

int addTwoNum(byte p, byte q);  //called forward declaration with argument list

void setup()
{
     Serial.begin(9600);
     int sum = addTwoNum(0x56, 0x78); //calling the UDF with actual arguments
     Serial.println(sum, HEX);  //shows: CE
}

void loop()
{

}

int addTwoNum(byte x, byte y) //actual arguments are taken via formal arguments
{
      int z = x + y;
      return(z);  //z will be copied into sum
}

you don't need to, you could.

C++ says that things need to be declared before you can use them.
it's fine to fully define the function before the setup :slight_smile:

int addTwoNum(int x, int y) //actual arguments are taken via formal arguments
{
      int z = x + y;
      return z;  //z will be sent back to the caller
}

void setup()
{
     Serial.begin(115200);
     int sum = addTwoNum(0x56, 0x78); // the result of the function call is copied into the sum variable
     Serial.println(sum, HEX);  //shows: CE
}

void loop() {}

Indeed!
But in Arduino environment even this is not necessary, because the IDE automatically generates declarations of all functions and inserts them at the beginning of the sketch:

thank you all

yes, but best to learn not to depend on that :slight_smile:
(it's working better than in the past but still has issues from time to time)

Functions in C++

My problem is different -- I don't deal with seasoned giant Software Engineers but with the kids who need complete spoon feeding for the first 20 classes and then they start picking up things during the last 22 classes of a semester. Therefore, I need to comply very much with the Language Rules.

In post #5, my operands are knowingly bytes with range 0x00 - 0xFF. The result will have the range: 0x00- 0x1FE. Therefore, the return type must be at least int. From this view point, my forward declaration of the UDF is:

int addTwoNum(byte p, byte q);

But, the above declaration has been re-formatted as follows in post #6. Is it necessary?

int addTwoNum(int p, int q);

No, because evaluating of

in any case will done in int type.

which was why I moved the parameters to int :slight_smile:

and I also fixed the typo there :slight_smile: and removed the useless parenthesis which makes students believe that return is a function call whereas it's a statement