Rust for the Novice Programmer/Functions

Functions
Let's say we wanted to do the x2 + 5 again but to y with a starting value of 7. What would that look like? This will work, but say we wanted to change it to x2 + 8 for both of them. This means we would have to change it in both locations. This doesn't seem like too much effort here, but these patterns of wanting to do the same things, just with slightly different values is very common. Therefore we can separate this out into a function. Let's take one of them first: This will work, except the point of a function is to take in input(s) that can have different values. The way to put an input into a function is to put it inside the brackets. You put the name of the input, similar to the name of a variable, then you have to explicitly write out the type of the input.

Note that we set value to the value of input. This means since we use the 'mut' keyword, we can modify the value inside the function. Alternatively we could put mut before 'input' and then input would be modifiable. Next we can change main to This means the code is a lot easier to modify and separate out, making it easier to read and edit later.

An alternative way might be if we wanted to not print out inside the function and instead output the different value, so that the main function can print it out or do anything it wants with it. This is called returning and you specify the return value with '-> type'. For example, the function will return an integer, so we return i32 by adding '-> i32' after the but before the {}. Then we print the values in the main function as so: This lets us do more interesting things, like chaining function calls together. For example we can change the main to: This does the x2 + 5 on 10 twice, so we get 11030. This has just been a rough overview of functions and their usefulness.

Next: if statements and booleans