Functions Flashcards

1
Q

How to define a function

A

fn function_name (param 1, param 2){

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How to invoke a function

A

let x: u32= 10;
print_age(x);

fn print_age(age: u32){
println!(“Age: {}”, age)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to return a value of a function

A

println!(“Pi value is: {}”, get_pi_value());

fn get_pi_value() -> f64{
22/7
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Parameters can be passed to a function in how many techniques

A

Pass by value
Pass by reference

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Example of pass by value

A

fn main(){
let no:i32 = 5;
mutate_no_to_zero(no);
println!(“The value of no is:{}”,no);
}

fn mutate_no_to_zero(mut param_no: i32) {
param_no = param_no*0;
println!(“param_no value is :{}”,param_no);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example is pass by reference

A

let mut age: u32 = 19;
age_next_year(& mut age);

println!(“Age next year: {}” age)
fn age_next_year(age: &mut u32){
*age +=1;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly