<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Function on Semonan Book</title><link>https://semonan.com/en/tags/function/</link><description>Recent content in Function on Semonan Book</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Mon, 02 Dec 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://semonan.com/en/tags/function/rss.xml" rel="self" type="application/rss+xml"/><item><title>Function</title><link>https://semonan.com/en/book/programming/rust/function/</link><pubDate>Mon, 02 Dec 2024 00:00:00 +0000</pubDate><guid>https://semonan.com/en/book/programming/rust/function/</guid><description>&lt;h1 id="function"&gt;Function&lt;a class="anchor" href="#function"&gt;#&lt;/a&gt;&lt;/h1&gt;
&lt;h2 id="function-definition"&gt;Function Definition&lt;a class="anchor" href="#function-definition"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Functions are created using the &lt;code&gt;fn&lt;/code&gt; keyword as follows.&lt;br&gt;
There is no need to consider the order in which functions are defined, as long as they are defined somewhere within a scope.&lt;br&gt;
Therefore, in the example code below, the &lt;code&gt;hello()&lt;/code&gt; function is located after the &lt;code&gt;main()&lt;/code&gt; function, and it compiles and runs correctly.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-rust" data-lang="rust"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;hello&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="fm"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;hello&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="parameter"&gt;Parameter&lt;a class="anchor" href="#parameter"&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;You can pass parameters when calling a function, as shown in the following example code.&lt;br&gt;
The &lt;code&gt;add&lt;/code&gt; function can take two parameters, &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, both of type &lt;code&gt;i64&lt;/code&gt;.&lt;br&gt;
You must declare the type of each parameter, like &lt;code&gt;i64&lt;/code&gt;, when receiving them in a function.&lt;br&gt;
You can pass multiple parameters.&lt;/p&gt;</description></item></channel></rss>