2016-04-11 3 views
0

Ich habe drei Funktionen genannt, zum Beispiel:Speicher Iteration Ergebnisse einer Funktion und mit ihnen, wenn sie von einer anderen Funktion

void firstFunc(entry) { 
    this function returns list of entries that are stored in an array 
} 

void secondFunc() { 
    while (loop x number of times) { 
     firstFunc(the value here gets updated with the next entry every loop); 
     compare the updated entry with another local parameter that is set within this method; 
     if lesser { do something; } 
    }  
} 

void thirdFunc() { 
    while (loop x number of times) { 
     firstFunc(the value here gets updated with the next entry every loop); 
     compare the updated entry with another local parameter that is set within this method; 
     if greater { do something; } 
    }  
} 

Hier „firstFunc“ wird zweimal durchgeschleift (einmal in „secondFunc“ und dann in " ThirdFunc "). Alles, was ich so etwas wie dies will:

void firstFunc(entry) { 
    this function returns list of entries that are stored in an array 
} 

while (loop x number of times) { 
     firstFunc(the value here gets updated with the next entry every loop); 
} 

void secondFunc() { 
    while (loop x number of times) { 
     use the value next updated value from firstFunc; 
     compare the updated entry with another local parameter that is set within this method; 
     if lesser { do something; } 
    }  
} 

void thirdFunc() { 
    while (loop x number of times) { 
     use the value next updated value from firstFunc; 
     compare the updated entry with another local parameter that is set within this method; 
     if greater { do something; } 
    }  
} 

In dem obigen Code wird der Looping von firstFunc einmal statt es in secondFunc und thirdFunc getan. Meine Frage ist, wie kann ich das erreichen?

+0

Meinen Sie, die Ergebnisse von 'firstFunc' zu speichern, so dass bei den gleichen Argumenten später nur die Ergebnisse des letzten Mals wiederverwendet werden? – JesseTG

+0

Oben werden Werte in "secondFunc" und dann wieder von Anfang an in "thirdFunc" iteriert. Das kostet zu viel Zeit. Ich will es einfach nicht von Anfang an in "thirdFunc" wiederholen. – Sara

+0

der Wert von b in secondFunc und thirdFunc ändert sich nie ... also, was fragt wieder? – Pemdas

Antwort

0

Sie können ein Array verwenden, um die Ergebnisse jedes firstFunc() zu memoisieren.

Bei der zweiten und dritten Funktion verwenden Sie den Wert aus dem Array, anstatt die Funktion erneut aufzurufen.

Verwandte Themen