Custom voids

Hello, I am new to programming Arduino, I know a bit of Java but not much, I was just wondering if there is way of doing something like this (look at code). Use a void for different variables or ports.

Thank you.

#include <Servo.h>
Servo test;

void setup(){
test.attach(9);
}

void loop(){
mapServo(A3, 9);
}

void mapServo(potPort,servoPort){ //potPort mean potentiometer port
double pot = analogRead(potPort);
double angle = map(potPort, 0, 1023, 0, 180);
test.write(angle);
}

You mean can you add functions other than setup() and loop()? If so, absolutely.

"void" refers to the fact that the function doesn't return anything to the caller. If you want a function to return a value like a char or an int you can specify it as, say:

char MyFunc( int index )
{
...

}//myFunc

or

int MyFunc1( void )
{
...

}//MyFunc1

You'd need to tell the compiler what the parameter type(s) are, so your:

void mapServo(potPort,servoPort){ ...

might actually be, say:

void mapServo( int potPort, int servoPort){ ...

Those are not voids, those are functions that don't return a value. Just like Java. ... and ...

Just like Java, you get to declare your own functions including what they return.

Unlike Java, functions have to be defined before they are used, or at least there needs to be a partial declaration that shows what the types of the arguments are and what the type of the returned value is.

vaj4088:
Unlike Java, functions have to be defined before they are used, or at least there needs to be a partial declaration that shows what the types of the arguments are and what the type of the returned value is.

Since the Arduino IDE does that automatically for you in the background for functions defined in your sketch's .ino files, I think it's an extremely unnecessary detail to dump on a beginner.

Sebastian28:
Hello, I am new to programming Arduino, I know a bit of Java but not much, I was just wondering if there is way of doing something like this (look at code). Use a void for different variables or ports.

As others have said the correct name for them is "function" rather than "void".

You may get some ideas from Planning and Implementing a Program

...R

Arduino functions.

pert, you are correct, but in some complex programs the Arduino IDE gets it wrong. This is one reason why I switched away from the Arduino IDE and switched to Sloeber and Eclipse, but I do have to use a header file to ensure that my functions are partially defined/declared prior to use.

I did not intend to confuse users of the Arduino IDE and I am sorry for that.

It is very true that the Arduino IDE fails at this sometimes. However, 99.999% percent of Arduino beginners never encounter that problem so I think it's best to wait until the 1 in 10000 reports that issue before explaining function prototypes to them.