How to pass a String into a function using call by value?
Last Updated :
02 Nov, 2023
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++
- Define a function that takes a string as a parameter.
- The function receives a copy of the original string, which is a local copy within the function.
- 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;
return 0;
}
|
- Define a function that takes a string as a parameter.
- The function receives a copy of the original string, which is a local copy within the function.
- 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);
}
}
|
- Define a function that takes a string as a parameter.
- The function receives a copy of the original string, which is a local copy within the function.
- 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)
|
- Define a function that takes a string as a parameter.
- The function receives a copy of the original string, which is a local copy within the function.
- 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);
|
- Define a function that takes a string as a parameter.
- The function receives a copy of the original string, which is a local copy within the function.
- 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);
}
}
|
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.
Share your thoughts in the comments
Please Login to comment...