Rust Programming : Functions ( Section-1 )

Assalamu'alaikum wr. wb


Hasil gambar untuk data type rustOn Rust, functions are pervasive. We've already seen one of the most important functions in the language: the main function which is the entry point of many programs. We've also seen the fn keyword which allows us to declare a new functions.

Rust code uses snack case as the conventional style for funtion and variable names. In sncak case, all letters are lowercase and underscores separate words. Here's a program that contains an example function definition :

fn main(){
println!("Hello world!");

another_function();
}

fn another_function(){


println!("This new statement on another_function!");


}

Function definitions in Rust start with fn and have a set of parentheses after the function name. The curly brackets tell the compiler where the function body begins and ends. We can call any function we've defined by entering its name followed by a set of parentheses. Because another_function is defined in the program, it can be called from inside the main funtion. Note! That we defined another_function after the main funtion in the source code; We could have defined it before as well. Rust doesn't care where we define our functions, only that they're defined somewhere.

  • Function Parameters 
Functions can also be defined to be have paramaters. which are special variables that are part's of a signature. When a function has a parameters we can provide it with concrete values for those parameters. Technically, the concrete values are called arguments, but in casual conversation, people tend to use the words paramaters and argument interchangeably for either the variable in a function's definition or the concrete values passed in when we call a function.

The following rewritten version of another_function shows what parameters loook like in Rust :



fn main(){
println!("Hello world!");

another_function(5);
}

fn another_function(x: i32){


println!("This value of x is {}", x);


}

The declaration of another_function has one parameter named x. The type of x is specified as i32. When 5 is passed to another_function, the println! macro puts 5 where the pair of curly brackets were in the format string.

In function signatures we must declare the type of each parameter. This is a deliberete decision in Rust's design, requiring type annotations in function definitions means the compiler almost never needs us to use them elsewhere in the code to figure out what us mean.

When we want a funtion to have multiple parameters, separate the parameter declarations with commas, like this :


fn main(){
println!("Hello world!");

another_function(5, 6);

}

fn another_function(i32: x, i32: y){


println!("The value of x {} : ", x);

println!("The value of y {} : ", y);

}

This example creates a function with two parameters, both of which are i32 types. The function then prints the values in both of its parameters. Note! That function parameters don't all need to be the same type, they just happen to be in this example.

.... To be continued .... :)

Because this section is to complex to discuss in one page, i split up this section for two sections guys.
See you on next section...
Selamat belajar....

Wassalamu'alaikum wr. wb
Ahmad Istakim

Alumni dari jurusan Manajemen Informatika di Universitas Sains Al-Qur'an (UNSIQ ) Wonosobo. Tertarik dalam bidang pendidikan, teknologi komputasi dan disiplin ilmu keislaman ( Tafsir, Hadits, Arudl, Nahwu-Sharaf, Fiqh maupun Aqidah ) - https://s.id/blog-islamQ. Pernah juga mengenyam pendidikan di beberapa pesantren yang ada di Kab. Wonosobo dan Kab. Purworejo

Posting Komentar

Lebih baru Lebih lama