I'm classically trained psychologist, not a programmer, so sometimes the more advanced aspects of programming escape me, in particular regarding program efficiency and/or certain best practices, in this case with regard to the use of variables.
Here is some pseudo-code:
var a;
var b;
var c;
function GetSomeInformation() {
returns "XYZ";
}
a = GetSomeInformation();
b = GetSomeInformation();
c = GetSomeInformation();
So my question is:
Is it more or less efficient (or the same) to store data into a variable once and reference that as opposed to repeated calls to the same function?
I.E., is this code more efficient?
var results = GetSomeInformation();
a = results;
b = results;
c = results;
If so, is this gain or loss in efficiency generally the same across languages, or does it vary by language? Are there thresholds where it becomes better to name a variable as opposed to using a repeated function call or vice versa? What aspects might change its efficiency (e.g., is there a difference whether it's a member function of a class vs. a regular function in the global scope)? etc.
If possible, I'd like to know specifically how such a notion applies to C++/MFC dialogs, as it arose when I was writing some code in that framework.
// define pointers to the items in my form
CListBox *pLISTBOX = (CListBox*) GetDlgItem(LISTBOX);
CStatic *pSTATIC_A = (CStatic*) GetDlgItem(STATIC_A);
CStatic *pSTATIC_B = (CStatic*) GetDlgItem(STATIC_B);
CEdit *pEDIT_BOX_A = (CEdit*) GetDlgItem(EDIT_BOX_A);
CEdit *pEDIT_BOX_B = (CEdit*) GetDlgItem(EDIT_BOX_B);
int SelectedIndex = pLISTBOX->GetCurSel();
pSTATIC_A->SetWindowText(pLISTBOX->GetItemData(SelectedIndex));
pSTATIC_B->SetWindowText(pLISTBOX->GetItemData(SelectedIndex));
pEDIT_BOX_A->SetWindowText(pLISTBOX->GetItemData(SelectedIndex));
pEDIT_BOX_B->SetWindowText(pLISTBOX->GetItemData(SelectedIndex));