Strings passed to a function unmodified in D

I am a long term programmer. I heard about D and decided to look into it. I love the possibilities it offers. I ran into a problem that puzzled me. I looked online and couldn't find an answer. I am trying to pass strings through a function:

module main;

import std.stdio;
import std.string;

int foobar(string s1, string s2)
{
    string t1="Hello";
    string t2="there";
    writeln("t1 = ",t1, " t2 = ", t2);
    s1=t1;
    s2=t2;
   writeln("s1 = ",s1," s2 = ",s2);
   return 0;
}

int main(string[] args)
{
    string a1;
    string a2;
    foobar(a1, a2);
    writeln("a1 = ",a1," a2 = ",a2);
    return 0;
}

      

The conclusion is as follows:

t1 = Hello t2 = there
s1 = Hello s2 = there
a1 =  a2 = 

      

I tried to search the internet for an answer if I may not find it. I suspect that I am simply not asking the correct question. I know I can do this using char strings, but I'm trying to do it "D way". Will anyone point me to a link that can help with this or tell me a question to ask?

If an answer was given earlier, I apologize. I probably didn't ask the right question.

Thanks in advance for your time.

Michael

+3


source to share


1 answer


Add ref

to your options (for example int foobar(ref string s1, ref string s2)

). A string is just a regular slice for immutable characters, so it is passed by value. If you want to change slices, you need to pass them by reference.



+6


source







All Articles