Function In Rust Programming

                                                         

                As we know from the Hello World program, the execution of a Rust program starts from the main function. Again, we can create a chain of function calls. When the execution of the main function ends, we can say that the program has stopped running. But, to end the main function, all functions which have been called from the main function should end first and this is true for every function call.

                                      



                    When we call a function, we can pass a list of values separated with a comma between ( and ) or we can leave it empty. The values passed in a function call are called parameters of the function. There is a name associated with a parameter and it has a certain data type e.g. i8, f32, and so on.

                             


                    A function can also return a value from inside to its caller. If a function does not return a value then by default () is returned. It is called unit type and it is an empty tuple. To return a value from a function, we can omit a semicolon in the last statement in the function. If we want to return before the last statement we have to use a return statement. We have to also specify the data type of the return value.

                  In this video, the plus_two function accepts a parameter named x, the data type of the parameter is i128 and the data type of the return value is i128. This function returns the value of the parameter plus two.

Happy reading. Keep in touch.

Comments

Popular posts from this blog

Programming Concepts Part I

Rust Hello World Program

Programming Concepts Part II