Introduction
A function gives a name to a piece of work so a program can call that work whenever it is needed. Functions can receive input through parameters and return a calculated value.
In this lab, a prepared Cargo project already contains three conversion functions and all their calls. You will replace one placeholder expression in each step. This keeps the typing small while showing how function definitions, parameters, return types, expressions, and calls fit together.
Return a Value from a Function
In this step, you will complete a function that converts minutes to seconds and connect its definition to its call.
Enter the prepared project and open its source file:
cd /home/labex/project/conversion-tool
nano src/main.rs
At the top, this line begins a function definition:
fn minutes_to_seconds(minutes: i32) -> i32 {
fnintroduces a function.minutes_to_secondsis the function's name.minutes: i32is a parameter: an input namedminuteswhose type is the integer typei32.-> i32says the function returns ani32value.- The braces contain the function body.
Rust checks every call against this signature. A caller must supply the expected number of arguments, in the declared order, and each argument must have a compatible type.
Replace only the 0 before the Step 1 comment with:
minutes * 60 // Step 1: replace this placeholder expression.
Notice that minutes * 60 has no semicolon. It is the final expression in the function body, so its value becomes the function's return value. Adding a semicolon would turn it into a statement that does not return this value.
Save with Ctrl+O, press Enter, and exit with Ctrl+X. Check the code:
cargo check
In main, minutes_to_seconds(5) is a function call. The value 5 is the argument supplied to the minutes parameter. Run the program:
cargo run --quiet
Its first line should now be:
5 minutes = 300 seconds
Use a Decimal Parameter and Return Type
In this step, you will complete a second function that accepts and returns floating-point values.
Open the source file:
nano src/main.rs
Find celsius_to_fahrenheit. Its parameter and return type are both f64, so its calculation can preserve a fractional result. The temporary expression celsius * 0.0 uses the parameter but deliberately returns zero. Replace that complete expression with:
celsius * 9.0 / 5.0 + 32.0 // Step 2: replace this placeholder expression.
This is another expression return: the calculated value is the last item in the function body and has no semicolon. Multiplication and division are evaluated before addition, following normal arithmetic precedence. Parentheses can make a more complicated intended order explicit. The caller does not need to know the formula; it supplies a Celsius argument and receives the returned Fahrenheit value.
Save, exit, and run the program:
cargo run --quiet
The second line should be:
20.0 C = 68.0 F
The call celsius_to_fahrenheit(20.0) passes one argument. Its returned value is stored in the fahrenheit binding before being formatted for display.
Reuse a Function with Multiple Arguments
In this step, you will complete one function with two parameters and observe two calls reuse it.
The prepared scale_amount function receives amount and factor. Parameters are separated by commas, and each parameter has its own type. A call must provide arguments in the same order.
Open the file and find the Step 3 placeholder:
nano src/main.rs
The temporary expression amount * factor * 0.0 deliberately returns zero. Replace that complete expression with:
amount * factor // Step 3: replace this placeholder expression.
Save and exit, then run a final check:
cargo check
Run the completed program:
cargo run --quiet
The same function is called once for flour and once for water. The final two lines should be:
Scaled flour: 375.0 g
Scaled water: 270.0 ml
This is the main benefit of a function: the calculation is defined once, given a clear name, and reused with different arguments.
Summary
You completed functions with integer and floating-point parameters, read return-type arrows, returned values with final expressions, passed arguments in function calls, and reused one function for multiple calculations.



