ADVERTISEMENT
ADVERTISEMENT

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.
Submitted by Nidhi, on October 06, 2021

Problem Solution:

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


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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

© https://www.includehelp.com some rights reserved.