Hey folks, welcome to part-5 of my blog series on rust. In this series we are learning rust programming language from scratch, if you haven't read the previous parts then i highly recommend to read those first so that you get the context of what we are doing here. Read here.
So without wasting any time lets get started!
Functions
If you haven't noticed, You've already seen the usage of functions, in fact you've used it (if you read the previous parts here).
If you guessed it then you're correct its the main
function , all of our code lives inside this main
function.
You've also seen the fn
keyword which allows us to write a new function.
Rust's convention is to write function and variable names in snake case (snake case is basically all lower case letters separated by underscores eg:
this_is_a_function_name
).
Lets write another function called printer function which will just print something in the terminal.
fn main() {
printer_function();
}
fn printer_function() {
println!("This function prints something!");
}
If you try to run the above code you'll see the statement printed in the terminal. Now lets see what all things are going on, skip the main function for now, below that we have declared a new function called printer_function
which has a body inside the curly braces, which basically prints a string in the terminal. For defining a new function we first need to write fn
keyword followed by a space followed by function's name (it can be anything) followed by a set of parenthesis and then a set of curly braces, inside this curly braces you can write whatever logic you want to run when this function is called.
Now lets go back to the main
function, what main
function is doing here is calling the printer_function
to call a function you just need to type the function's name and followed by a set of parenthesis.
Note: We have defined
printer_function
after themain
function thats not necessary we could've defined it before as well, rust simply doesn't care where you've defined your functions. Only thing that matters is its inside the scope that can be seen by the caller.
Parameters
We can also define functions to have some parameters
, which are basically special variables that are part of a function's signature. When a function has parameters you can provide it with concrete values for those parameters. Technically, it is called arguments
, but in the real world devs use parameters and arguments interchangeably.
Lets see how a function with parameters looks like:
fn main() {
printer_function(5);
}
fn printer_function(x: i32) {
println!("The value of parameter x is {x}");
}
Try running this program you'll see it ill print the given string in the terminal and the value of x will be 5.
Now lets understand how this is working, when we declared the printer_function
we gave it a parameter inside the parenthesis called x
(you can name it anything) which is of data type i32
(Don't know what a data type is? Check out my blog on data types in rust here), and we just use that parameter x
in our functions logic.
And also if you see inside the main
function where we called the printer_function
we gave it a number 5 inside the parenthesis which is of type i32
, and we have to give it a variable of type i32
only because we mentioned this in our functions parameter signature, now try to give it a string instead of a i32
, you'll get an error because you have to provide a concrete value of the same data type with you mentioned in the functions parameters.
Statements and Expressions
Now this can be a bit confusing, if you're just starting with rust atleast for me it was hard to understand in the first try, lets try to simplify this as much as possible.
A function's body is made up of series of statements
and the last line is basically called expression
. Expression is basically like a return
line, if you're familiar with coding and function then you know that a return statements just ends a function and that is called an expression in rust.
Lets understand a bit what is the difference between the two because this is a bit different from other programming languages:
Statements: These are instructions that perform some action and do not return a value.
Expressions: These evaluate to a resultant value.
Lets understand this with a example.
We've actually used statements in many places before, for example,
let x = 100;
This is a statement in itself, there is a simple trick to understand it better all the lines ending with a semi-colon are statements and one which does not have semi-colon in the end is an expression but that should be last line of function's body.
Now if we try something like following:
fn main() {
let x = 100
println!("The value of x is {x}");
}
Then we'll get an error because we can't write expressions in the middle of a function's body here the function is main
we have to write the expressions in the end.
If we try to run the following code:
fn main() {
let x = 100;
println!("The value of x is {x}")
}
It will work perfectly fine, because expressions should be last line of the function.
Now you might be thinking that what if there are some conditionally situations in which i want to terminate a function which is pretty common in programming for that you can use the return
keyword to terminate the function like following:
fn main() {
fn another_function() -> i32 {
let condition = true;
if condition {
return 10;
} else {
return 50;
}
}
let x = another_function();
println!("The value of x is {x}");
}
In the above example the value of x will be 10 because condition will always be true, one imp thing if you directly try to return something without specifying i32 or whatever is the return type of your function then you'll get an error.
Learn with more examples on statements and expressions here.
Comments
This topic is in almost all programming languages, and its a very easy one and if you're even a little bit familiar with coding then there are high chances you might actually know this.
Comments are basically lines which are ignored by your compiler. Now you might be thinking then why do we need them, sometimes when you want to explain your code you leave comments just for some more explanations for the person reading your code.
Here’s a simple comment:
// Hello, world!
You write a comment by adding two forward slashes and then write whatever you want after a space. That particular line is ignored by compiler. You can even write comments after a code line like following
let y = 7; // this assigns a value of 7 to variable y.
The comment will be ignored by the compiler and rest code will run as usual.
Read more about comments here.
That's It!
That was a pretty big blog, i hope you liked it and learned something new, if you did then please react to this blog and leave a comment if you have any doubts or suggestions.
In the next blog we'll see control flow in rust. See you guys in the next one, Till then!
Happy Coding :)
You can find me on twitter here.
Top comments (0)