Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

410
Views
¿Por qué esta función devuelve 83 y no 5 cuando llamamos a la función (2)?

Cuando ejecuto esta función ingresando 2 y no entiendo por qué devuelve 83 y no 5, ya que debería llamar a la función (1) en la declaración else y luego la declaración if debería ejecutarse y la función debería devolver 5.

 public static int function(int y) { if (y == 1) return 5; function(y - 1); y = y + 1; return 83; }
over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

Devuelve 83 porque no está utilizando el valor de retorno de la function(y - 1);
Para probarlo prueba:

 public static int function(int y) { if (y == 1) return 5; else { int returnedValue = function(y - 1); System.out.println("un used returned value: "+returnedValue); y = y + 1; return 83; } }

Cambiar a:

 public static int function(int y) { if (y == 1) return 5; /*else*/ return function(y - 1); }
over 4 years ago · Santiago Trujillo Report

0

Con entrada: y = 2,

otra condición será manejada.

 function(y - 1);//nothing impact to y y = y + 1;// y =3 return 83; // but finally return 83

¿Entiendes el punto?

over 4 years ago · Santiago Trujillo Report

0

Dado que no devuelve function(y-1) , la declaración es tan buena como anular la función (para esa llamada).

Entonces, tan pronto como el control alcanza la función (y-1) y finalmente conduce a la función de llamada (1), obtiene 5 pero no se devuelve a la función del controlador. En cambio, lo que se devuelve es el valor 83.

over 4 years ago · Santiago Trujillo Report

0

Mira esto:

 public static int function(int y) { if (y == 1) return 5; function(y - 1); // you do not use return value y = y + 1; return 83; // you always get this return }

Significa que puede reemplazar su función con:

 public static int function(int y) { return 83; }

Parece que quieres hacer algo. Me gusta esto:

 public static int function(int y) { return y == 1 ? 5 : function(y - 1); }

pero tenga cuidado, porque para y < 1 tendrá una recursividad infinita ( StackOverflowError ).

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!