functions not passing info back

Newbie McNewbie here.

Trying to figure out why the function doesn't pass back a value to the main loop. I have read and read, and tried. The value gets passed to the function but doesn't appear to get passed back.

I put it into a simple loop to try to figure out why it's not working. I am trying to get the %xx hex code that gets sent from a web page turned back into the character that was sent. The code may be goofy but I am still in the learning stages.

Obviously being a bonehead. Can anyone shed some light as to what I'm missing?

String newval;
String req="/a?usrpass=tomatosoup%21&ename=banana%21&empass=flipflop%3D";
void setup()         
{
Serial.begin(115200);


}   
void loop()
{
    Serial.print(" req =");
    Serial.println(req);
    newval = req.substring(req.indexOf('=') + 1, req.indexOf('&'));  //get input
    Serial.print(" newval ="); Serial.println(newval);
    String new2;
    int l;
     new2 = changechar(newval);
    
    Serial.print("new2 = "); Serial.println(new2);
    newval=new2;
    if (newval == "&")newval = "";  //.................No value=""
    l = newval.length(); //check length
    if (l == 0) l = 1;  //too short so mqake l=1
    Serial.print("newval = "); Serial.println(newval);
    req = req.substring(req.indexOf('&') + 1);  
}



String changechar(String newval1)   //......................................Change character
{
  //stuff to remove the digits from req every time

  //String schar;
  if (newval1.indexOf('%') > 0) {
    Serial.print("newval = "); Serial.println(newval1);
    Serial.print("Location of % = "); Serial.println(newval1.indexOf('%'));
    String schar = newval1.substring(newval1.indexOf('%') + 1, newval1.indexOf('%') + 3);
    String nschar;
    long A = strtol(schar.c_str(), NULL, 16);
    nschar = (char) A;
    
    Serial.println();
    Serial.print("Schar="); Serial.println(schar);
    Serial.print(" nschar= "); Serial.println(nschar);
    
    String newreq = "";
    newreq = newval1.substring(0, newval1.indexOf('%') );
    newreq += nschar;
    newreq += newval1.substring(newval1.indexOf('%') + 3);
    Serial.print("newchar= "); Serial.println(newreq);
    newreq = newreq;
  }
  return newval1;
}

Output=
þ req =/a?usrpass=tomatosoup%21&ename=banana%21&empass=flipflop%3D
newval =tomatosoup%21
newval = tomatosoup%21
Location of % = 10

Schar=21
nschar= !
newchar= tomatosoup!
new2 = tomatosoup%21
newval = tomatosoup%21
req =ename=banana%21&empass=flipflop%3D
newval =banana%21
newval = banana%21
Location of % = 6

Schar=21
nschar= !
newchar= banana!
new2 = banana%21
newval = banana%21
req =empass=flipflop%3D
newval =flipflop%3D
newval = flipflop%3D
Location of % = 8

Schar=3D
nschar= =
newchar= flipflop=
new2 = flipflop%3D
newval = flipflop%3D

(deleted)

    newreq = newreq;

What is this supposed to be doing?

The value gets passed to the function but doesn't appear to get passed back.

Could you be a little less vague? What value gets passed to what function?

It appears that you are passing a String to changechar(), and printing the return value from the function, successfully. So, it is difficult to understand your issue.

All of my code that calls functions includes two statements in the function:

String changechar(String newVal1)
{
   Serial.println("changechar ==>");
   <snip>
   Serial.println("changechar <==>);
   return newVal1;
}

Since you never assign a new value to newVal1, I can't see why it is what you want to return. The caller already knows the value in that variable.

Your variable, and argument, names are completely useless. There is no way that the input to the function changechar is a new anything.

Does this clarify things?

String StringToPassToFunction;
String requestFromWeb="/a?usrpass=tomatosoup%21&ename=banana%21&empass=flipflop%3D";
void setup()         
{
Serial.begin(115200);


}   
void loop()
{
    Serial.print(" requestFromWeb =");
    Serial.println(requestFromWeb);
    StringToPassToFunction = requestFromWeb.substring(requestFromWeb.indexOf('=') + 1, requestFromWeb.indexOf('&'));  //get input
    Serial.print(" StringToPassToFunction ="); Serial.println(StringToPassToFunction);
    String NewValueReturnedfromFunction;
    
    NewValueReturnedfromFunction = changechar(StringToPassToFunction);
    
    Serial.print("NewValueReturnedfromFunction = "); Serial.println(NewValueReturnedfromFunction);
    requestFromWeb = requestFromWeb.substring(requestFromWeb.indexOf('&') + 1);  
}



String changechar(String StringFromMainLoop)   //......................................Change character
{
  //stuff to remove the digits from req every time

  //String specialCharacter;
  if (StringFromMainLoop.indexOf('%') > 0) {
    Serial.print("StringFromMainLoop = "); Serial.println(StringFromMainLoop);
    Serial.print("Location of % = "); Serial.println(StringFromMainLoop.indexOf('%'));
    String specialCharacter = StringFromMainLoop.substring(StringFromMainLoop.indexOf('%') + 1, StringFromMainLoop.indexOf('%') + 3);
    String newSpecialCharacter;
    long A = strtol(specialCharacter.c_str(), NULL, 16); //Convert to a long integer
    newSpecialCharacter = (char) A;  //change integer to a character
    String newStringToPassBack = "";
    newStringToPassBack = StringFromMainLoop.substring(0, StringFromMainLoop.indexOf('%') );  //get part of string before %. 
    newStringToPassBack += newSpecialCharacter;                                               //Add the new character ! or whatever
    newStringToPassBack += StringFromMainLoop.substring(StringFromMainLoop.indexOf('%') + 3);  //add any thing behind the %21 or whatever special chacter
    Serial.print("newStringToPassBack= "); Serial.println(newStringToPassBack);
    StringFromMainLoop = newStringToPassBack; //put the whole string back into the string from the main loop with the ! in it
    }
    
  return StringFromMainLoop;
}

Why do you want to trash the original string? If that is really what you want to do, why does the function need to return anything? Back in loop, NewValueReturnedfromFunction and StringToPassToFunction will contain the same thing.

You can take meaningful names to extremes. 8)

Does returning the proper value from the function correct your original problem?

I understand that %21 and ! are the same thing. 33 decimal, %21 Hex, Octal 041, web &#33 are all Character ! according to the ascii table I'm looking at.

When web page value is submitted by a get statement, instead of getting ! it sends a %21. I want to convert that hex number to it's character equivalent so I can use it to populate a web field on a web page, or write it to an eeprom as part of a password for example, If I send %21 to a web page it'll show in the field as %21 not !. If I write to an eeprom it'll use 3 bytes as %21 and 1 byte as !.

My problem is understanding why newStringToPassBack shows as a ! but when I put that string back into StringFromMainLoop (StringFromMainLoop=newStringToPassBack;) and return StringFromMainLoop it shows as %21. It's like it hasn't changed. So am I passing the string back correctly?

Thanks!

It looks like I've solved it. I am not sure what I did but it now works. I must have changed something small and insignificant, but that's all it takes to go from hero to zero!
Here's the fixed code. I am going to work on it to reduce the number of variables.

String StringToPassToFunction;
String requestFromWeb="/a?usrpass=tomatosoup%21&ename=banana%21&empass=flipflop%3D";
void setup()         
{
Serial.begin(115200);


}   
void loop()
{
    Serial.print(" requestFromWeb =");
    Serial.println(requestFromWeb);
    StringToPassToFunction = requestFromWeb.substring(requestFromWeb.indexOf('=') + 1, requestFromWeb.indexOf('&'));  //get input
    Serial.print(" StringToPassToFunction ="); Serial.println(StringToPassToFunction);
    String NewValueReturnedfromFunction;
    
    NewValueReturnedfromFunction = changechar(StringToPassToFunction);
    
    Serial.print("NewValueReturnedfromFunction = "); Serial.println(NewValueReturnedfromFunction);
    requestFromWeb = requestFromWeb.substring(requestFromWeb.indexOf('&') + 1);  
}



String changechar(String StringFromMainLoop)   //......................................Change character
{
  //stuff to remove the digits from req every time

  //String specialCharacter;
  if (StringFromMainLoop.indexOf('%') > 0) {
    Serial.print("StringFromMainLoop = "); Serial.println(StringFromMainLoop);
    Serial.print("Location of % = "); Serial.println(StringFromMainLoop.indexOf('%'));
    String specialCharacter = StringFromMainLoop.substring(StringFromMainLoop.indexOf('%') + 1, StringFromMainLoop.indexOf('%') + 3);
    String newSpecialCharacter= "";
    long A = strtol(specialCharacter.c_str(), NULL, 16); //Convert to a long integer
    newSpecialCharacter = (char) A;  //change integer to a character
    String newStringToPassBack;
    newStringToPassBack = StringFromMainLoop.substring(0, StringFromMainLoop.indexOf('%') );  //get part of string before %. 
    newStringToPassBack += newSpecialCharacter;                     //Add the new character ! or whatever
    newStringToPassBack += StringFromMainLoop.substring(StringFromMainLoop.indexOf('%') + 3);  //add any thing behind the %21 or whatever special chacter
    Serial.print("newStringToPassBack= "); Serial.println(newStringToPassBack);
    StringFromMainLoop = newStringToPassBack; //put the whole string back into the string from the main loop with the ! in it
    }
    
  return StringFromMainLoop;
}

I am going to work on it to reduce the number of variables.

You might want to consider reworking it not to use Strings too.

I must have changed something small and insignificant

It may have been small, but, clearly, it was NOT insignificant.

Now I've fixed it I can now streamline it.

Thanks for the help. Sometimes just asking for help and describing the problem can help to clarify it in your brain(or lack thereof!).

I know the pros just see spaghetti code, but that's how ya learn!