Open In App

How to pass a String into a function using call by value?

Last Updated : 02 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In various programming languages, strings are typically passed by value, which means that the function receives a copy of the original string, and any modifications made within the function do not affect the original string. Here are the steps and functions involved in passing a string by value in C++, C#, Python, JavaScript, and Java:

Steps to pass a String into a function using call by value in C++

  1. Define a function that takes a string as a parameter.
  2. The function receives a copy of the original string, which is a local copy within the function.
  3. Any modifications made to the string within the function do not affect the original string.

Below is the implementation in C++:

C++




#include <iostream>
#include <string>
  
void modifyString(std::string str)
{
    str = "Modified string";
}
  
int main()
{
    std::string myString = "Original string";
    modifyString(myString);
  
    std::cout << myString
              << std::endl; // Output: "Original string"
  
    return 0;
}


Output

Original string

Steps to pass a String into a function using call by value in C#

  1. Define a function that takes a string as a parameter.
  2. The function receives a copy of the original string, which is a local copy within the function.
  3. Any modifications made to the string within the function do not affect the original string.

Below is the implementation in C#:

C#




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


Output

Original string

Steps to pass a String into a function using call by value in Python

  1. Define a function that takes a string as a parameter.
  2. The function receives a copy of the original string, which is a local copy within the function.
  3. Any modifications made to the string within the function do not affect the original string.

Below is the implementation in Python:

Python3




def modify_string(str):
    str = "Modified string"
  
my_string = "Original string"
modify_string(my_string)
  
print(my_string)  # Output: "Original string"


Output

Original string

Steps to pass a String into a function using call by value in JavaScript

  1. Define a function that takes a string as a parameter.
  2. The function receives a copy of the original string, which is a local copy within the function.
  3. Any modifications made to the string within the function do not affect the original string.

Below is the implementation in Javascript:

Javascript




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


Output

Original string

Steps to pass a String into a function using call by value in Java

  1. Define a function that takes a string as a parameter.
  2. The function receives a copy of the original string, which is a local copy within the function.
  3. Any modifications made to the string within the function do not affect the original string.

Below is the implementation in Java:

Java




public class StringModificationExample {
    static void modifyString(String str) {
        str = "Modified string";
    }
  
    public static void main(String[] args) {
        String myString = "Original string";
        modifyString(myString);
  
        System.out.println(myString); // Output: "Original string"
    }
}


Output

Original string

Conclusion:

In each of these programming languages, you pass a string by value, which means that the function works with a copy of the original string, and any changes made within the function do not impact the original string.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads