Bring the Return of a Function to a variable of another Function

Asked

Viewed 63 times

3

I would like to know if there are possibilities to do this:

function trazerNumero(){
    return 7;
}

function mostrarNumero(){
    numero = trazerNumero();
    alert(numero);
}



$(function(){
    mostrarNumero()
})

I know you will talk that it is better to make a single method, but in what I want not to do this, I need to take a value that will be generated in another Function and use in another Function.

  • May pass the return of trazerNumero as an argument for mostrarNumero.

1 answer

2

What questions is perfectly valid and if you test works.

To have more modular code would make sense, as Renan said, pass the value already as argument so:

function trazerNumero(){
    return 7;
}

function mostrarNumero(numero){
    alert(numero);
}

$(function(){
    var numero = trazerNumero();
    mostrarNumero(numero);
})
  • But in my need would have to be like this, it was just a basic example this, but let it be quiet I managed to find a way here. Thank you!

  • @Romariopires if you explain better the code you have and have not shown in the question I can help more and/ or give ideas.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.