Rust program to demonstrate the call by value parameter passing

Rust | Function Example: Write an example to demonstrate the call by value parameter passing.
Submitted by Nidhi, on October 06, 2021

Problem Solution:

In this program, we will create a user-defined function Swap() to swap numbers using call-by-value parameter passing. But if we pass the parameter using call by value then changes made in function do not reflect outside the function.

Program/Source Code:

The source code to demonstrate the call by value parameter passing is given below. The given program is compiled and executed successfully.

// Rust program to demonstrate the 
// call by value parameter passing

fn Swap(mut num1:i32, mut num2:i32) {
    let mut temp:i32 = 0;
    temp = num1;
    num1 = num2;
    num2 = temp;

fn main() {
    let mut num1:i32 = 10;
    let mut num2:i32 = 20;
    println!("Numbers before swapping: \nnum1:{0}\nnum2:{1}", num1, num2);
    println!("Numbers after swapping: \nnum1:{0}\nnum2:{1}", num1, num2);


Numbers before swapping: 
Numbers after swapping: 


In the above program, we created two functions Swap() and main(). The Swap() function is a user-defined function that accepts two parameters using a call-by-value mechanism and exchanges the value of variables inside the function result will not be reflected outside the function.

In the main() function, we created two integer variables num1, num2 which were initialized with 10, 20 respectively. Then we called the Swap() function with arguments num1 and num2 and printed the result.

Rust Functions Programs »



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.