Open In App

How to Pass a String to a Function using Call by Reference?

Last Updated : 30 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Passing a string by reference in various programming languages involves using specific mechanisms or constructs to allow the function to modify the original string directly, rather than working with a copy. Here, I’ll explain how to achieve this in C++, C#, Python, and JavaScript.

How to Pass a String to a Function using Call by Reference in C++

In C++, you can pass a string by reference using the ‘&' symbol in the function parameter list. This allows you to modify the original string directly within the function.

For Example:

C++




#include <iostream>
#include <string>
 
// using '&' symbol before the name of the string
void modifyString(std::string& str)
{
    str = "Modified string";
}
 
int main()
{
    std::string myString = "Original string";
    modifyString(myString);
 
    std::cout << myString
              << std::endl; // Output: "Modified string"
 
    return 0;
}


Output

Modified string

How to Pass a String to a Function using Call by Reference in Java

You cannot pass a string by reference directly, as Java is a pass-by-value language, and strings are immutable. However, you can achieve a similar effect by using mutable data structures or by encapsulating the string in a custom object.

Using a Mutable Data Structure (StringBuilder)

You can use the StringBuilder class, which is a mutable sequence of characters, to modify a string-like object and pass it to a function. Although this isn’t strictly passing a string by reference, it allows you to achieve the desired effect of modifying the original string-like object.

Steps:

  1. Create a StringBuilder object with the initial string.
  2. Pass the StringBuilder object to the function.
  3. Modify the StringBuilder object within the function.
  4. Access the modified string from the StringBuilder object after the function call.

Example:

Java




public class StringModificationExample {
    static void modifyString(StringBuilder strBuilder) {
        strBuilder.append(" (Modified)");
    }
 
    public static void main(String[] args) {
        StringBuilder myStringBuilder = new StringBuilder("Original String");
        modifyString(myStringBuilder);
 
        String myString = myStringBuilder.toString();
        System.out.println(myString); // Output: "Original String (Modified)"
    }
}


Output

Original String (Modified)

How to Pass a String to a Function using Call by Reference in C#

In C#, you can pass a string by reference using the ref' keyword in the function parameter list. This allows you to modify the original string directly within the function.

Example:

C#




using System;
 
class Program {
    static void ModifyString(ref string str) {
        str = "Modified string";
    }
 
    static void Main() {
        string myString = "Original string";
        ModifyString(ref myString);
 
        Console.WriteLine(myString); // Output: "Modified string"
    }
}


Output

Modified string

How to Pass a String to a Function using Call by Reference in Python

In Python, strings are immutable, so you cannot pass them by reference. However, you can work around this limitation by passing a mutable data structure, such as a list, and then converting it back to a string if needed.

Example:

Python




def modify_string(string_list):
    string_list[0] = "Modified string"
 
my_string_list = ["Original string"]
modify_string(my_string_list)
 
my_string = my_string_list[0]
print(my_string)  # Output: "Modified string"


Output

Modified string

How to Pass a String to a Function using Call by Reference in JavaScript

In JavaScript, you can achieve a similar effect by passing an object with a property that holds the string. You can then modify the property within the function.

Example:

Javascript




function modifyString(obj) {
    obj.str = "Modified string";
}
 
const myObj = { str: "Original string" };
modifyString(myObj);
 
const myString = myObj.str;
console.log(myString); // Output: "Modified string"


Output

Modified string

In these programming languages, you can pass strings by reference (or a similar effect) using language-specific techniques. However, it’s essential to be aware of the language’s rules and data structures, as string immutability in Python and JavaScript can impact the approach you take.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads