Getting a weird value replacing a variable after a function.

Hello everyone,
I'm having a hard time getting a variable to come out of my search function correctly. I am pulling from a text file that has integer values for a, b, and c but no d. I define all 4 of the variables at the very beginning and a, b, c get written over after the search function is executed. I want my default value for d to be used if it doesn't show up in my text file. I want to still look in the function for d in the event that my text file is updated and d gets entered. Right now I have it "hardcoded" so that I get the default value for d when it is not in the text file but what if I change the variable name or add more variables to the function? Each of these new variables would have their own default value and I don't want them to be written over by d in the event they also are not in the text file. I tried methods of just jumping out of the program if d isn't present but it prints a very high number(546) instead of the default for d. I have been trying StringToInt to no avail. Any tips?

Thank you,
Kelsey

#include <SPI.h>
#include <SD.h>

const int chipSelect = 53;
int a = 10;
int b = 20;
int c = 30;
int d = 40;
void setup(){
  Serial.begin(9600);
  if(!SD.begin(chipSelect)){
    Serial.println("card failed:");
  }
  else{
    Serial.println("card loaded:");
    a = search("a");
    Serial.println(a);
    c = search("c");
    Serial.println(c);
    b = search("b");
    Serial.println(b);
    d = search("d");
    Serial.println(d);
  }
}

int search(String temp){
  File myFile = SD.open("D.txt");
  char test[3];
  temp.toCharArray(test,2);
  boolean presence = myFile.find(test);
  if (presence == 1)
  { 
  int Q = myFile.parseInt();  
  return Q;
  myFile.close();
  }
  else if (presence == 0)
  {      
    return d;      //only works for D. need to expand for all.
    myFile.close();
    
  }
}

void loop(){
}

Not the cause of the problem (no idea about that).

After you return a value, the close() will not be executed. E.g.

    return d;
    myFile.close(); // <---- WILL NEVER BE EXECUTED

You need to swap the two statements around if you want to close the file.

Thanks,

I'll change that in my code. I'm very new to any programming so all the little things still trip me up.

-Kelsey

You are passing a string to the function. Why is the function then defined to accept a String? Why would it expect a String that you then have to unwrap to get at the string?

  boolean presence = myFile.find(test);
  if (presence == 1)

Boolean variables are usually compared to true or false. Why don't you?

If you don't want to overwrite the value for a variable, if search() fails to find the value in the file, you need to return two values (was the string found? what was the value associated with it?). That requires the use of reference variables or structs.