In this lesson we'll create a new function that receives parameters and called inside our program's main() function.
Pascal Precht: Let's say we want to create a function that takes a first name and a last name and prints it to the screen. To define a function, we use the fn keyword followed by a name, and then we define the parameters, in our case it's the first_name of type string and the last_name of type string as well.
We then define the function body, which is going to be println! With a placeholder and another placeholder followed by the first_name and last_name. This function takes a first_name, which is of type string, and a last_name, which is of type string, and will then output the first_name and the last_name using println! And the placeholder syntax.
We can then go ahead and call this function by using same name inside our main function. We pass it first and last. Save the file and run the program. We'll see it outputs "Pascal Precht."
You must be a Pro Member to view code
Access all courses and lessons, track your progress, gain confidence and expertise.