In which cases should I use a void function? Because I understand that if it does not return an answer to the main it is void ... but I do not understand in which cases to apply it and in which it does not. I decide? Do you have an application rule?
In which cases should I use a void function? Because I understand that if it does not return an answer to the main it is void ... but I do not understand in which cases to apply it and in which it does not. I decide? Do you have an application rule?
(Every method declares a body inside which carries code that will execute when the method is called, but the void
methods DO NOT return any value unlike the others int
, double
, bool
, char
, string
if they do it)
Well, first of all and first of all, you must understand a little about what they are and how methods are used.
What is a method?
Explained in my own words, the methods are the members of the classes that allow to call blocks of code (that is to say, they declare a body with code inside them and that when calling them that code is executed) apart from the class constructor
public void Metodo(parametro 1, parametro 2, parametro N)
{
//Cuerpo del método
}
Every method will execute the code it has in its body , but not all methods are the same ... there are methods that return a value:
int MetodoA()...
bool MetodoB()...
string MetodoC()...
char MetodoD()...
object MetodoZ()...
All of these require you to use the keyword return
within your body (and at the end of all code within the body) to indicate what value this method will return.
On the other side of the coin we have the empty methods, the void
, which as its name indicates, do not return any value. Voids also carry code inside their bodies, but they do not return any value
void MetodoVacio()...
Having said that, I'll give you an example:
For example, you have a class called MiClase
that contains several methods ...
public class MiClase
{
public MiClase(Puedes poner parametros, si quieres ya que no es obligatorio)
{
//Este es el constructor de la clase, aquí
//puedes poner código que se ejecutará al instanciar a la clase
}
public void MetodoVacio(Puedes poner parametros, si quieres ya que no es obligatorio)
{
//Este es un método que NO retorna valor alguno, puedes poner
//código en su cuerpo o no, pero nunca podrá retornar valores;
//curiosamente, si puedes usar return dentro de este tipo de método
//pero NO podrás indicar un valor que retornar
return;
}
public int MetodoEntero(Puedes poner parametros, si quieres ya que no es obligatorio)
{
//Este es un método que retorna un valor entero, puedes poner
//código en su cuerpo o no, pero siempre tendrá que retornar
//un valor entero, en este caso he puesto 27
return 27;
}
public string MetodoString(Puedes poner parametros, si quieres ya que no es obligatorio)
{
//Este es un método que retorna una cadena de texto, puedes poner
//código en su cuerpo o no, pero siempre tendrá que retornar
//un valor string, en este caso he puesto Hola Mundo
return "Hola Mundo";
}
public char MetodoCaracter(Puedes poner parametros, si quieres ya que no es obligatorio)
{
//Este es un método que retorna un caracter, puedes poner
//código en su cuerpo o no, pero siempre tendrá que retornar
//un caracter, en este caso he puesto el caracter A
return 'A';
}
//Y así sucesivamente
}