Q:

Rust program to create a user-defined function to add two integer numbers

belongs to collection: Rust Functions Programs

0

In this program, we will create a user-defined function addition() that accepts two integer variables as arguments and return the addition of numbers to the calling function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to create a user-defined function to add two integer numbers is given below. The given program is compiled and executed successfully.

// Rust program to create a user-defined function
// to add two integer numbers

fn addition(num1:i32, num2:i32)->i32{
    return (num1+num2);
}

fn main() {
    let num1:i32 = 10;
    let num2:i32 = 20;
    let mut res:i32 = 0;
    
    res = addition(num1,num2);
    
    println!("Addition is: {}",res);
}

Output:

Addition is: 30

Explanation:

In the above program, we created two functions addition() and main(). The addition() function is a user-defined function, which is used to add two integer numbers and return the addition of numbers to the calling function.

In the main() function, we created three integer variables num1num2, and res, which were initialized with 10, 20, and 0 respectively. Then we called the addition() function with arguments num1 and num2 and assigned the result to the res variable. After that, we printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to demonstrate the call by value para... >>
<< Rust program to create a simple function...