Pass by Reference
In this lesson, passing arguments by reference will be introduced to you.
We'll cover the following...
Arguments Pass by Reference
When we want the called function to make changes to the parameters such that the changes are seen by the calling function when the call returns. The mechanism to do this is called pass arguments by reference.
Syntax
The general syntax of passing arguments by reference is:
Example
The following example makes a function square()
that takes a number n
which is being passed by reference as a parameter to the function and prints the square of the function within the function.
Press + to interact
fn square(n:&mut i32){*n = *n * *n;println!("The value of n inside function : {}", n);}fn main() {let mut n = 4;println!("The value of n before function call : {}", n);println!("Invoke Function");square(&mut n);println!("The value of n after function call : {}", n);}
...