2016-04-11 10 views
0

は、私は3つの機能を持っている別の関数によって呼び出されたときにそれらを使用して:例えば、機能の繰り返し結果を格納し、

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; } 
    }  
} 

をここで、「firstFuncは」 "にし、「secondFunc」で1回2回(ループとされますthirdFunc ")。私が欲しいのは、このようなものです:

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; } 
    }  
} 

上記のコードで、firstFuncのループがsecondFuncとthirdFuncでそれをやってするのではなく、一度に行われます。私の質問は、これをどうすれば達成できるのか?

+0

'firstFunc'の結果を保存して、後で同じ引数を与えても、前回の結果を再利用するのですか? – JesseTG

+0

上記の値は "secondFunc"で反復され、 "thirdFunc"の先頭から値が再取得されます。これは時間がかかりすぎている。私はちょうど "thirdFunc"の最初から再度iterateしたくない。 – Sara

+0

secondFuncとthirdFuncのbの値は決して変更されません...何を再度尋ねていますか? – Pemdas

答えて

0

配列を使用して、各firstFunc()の結果をメモすることができます。

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

第2および第3の関数では、関数を再度呼び出す代わりに配列の値を使用します。

関連する問題