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

Rust | Function Example: Write an example to create a function to add two integer numbers.
Last Updated : October 06, 2021

Problem Statement

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.

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 num1, num2, 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.

Rust Functions Programs »



Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.