﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Go Program - The First internet site for Go programming &#187; Functions</title>
	<atom:link href="http://www.go-program.com/category/basics/functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.go-program.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 19 Nov 2009 11:31:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Go &#8211; Functions</title>
		<link>http://www.go-program.com/go-functions/</link>
		<comments>http://www.go-program.com/go-functions/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 16:38:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Functions]]></category>

		<guid isPermaLink="false">http://www.go-program.com/?p=36</guid>
		<description><![CDATA[







Functions
Functions are introduced by the func keyword.
Return type, if any, comes after parameters. The return does as you expect.
func square(f float) float { return f*f }
A function can return multiple values. If so, the return types are a parenthesized list.
func MySqrt(f float) (float, bool) {
if f &#62;= 0 { return math.Sqrt(f), true }
return 0, false
}
Functions [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p><script type="text/javascript"><!--
google_ad_client = "pub-9004887195759386";
/* Go programs text ads1 */
google_ad_slot = "3650915252";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script><br />
<script type="text/javascript"><!--
google_ad_client = "pub-9004887195759386";
/* Goprograms1 */
google_ad_slot = "8587802801";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p>Functions</p>
<p>Functions are introduced by the func keyword.</p>
<p>Return type, if any, comes after parameters. The return does as you expect.</p>
<p>func square(f float) float { return f*f }</p>
<p>A function can return multiple values. If so, the return types are a parenthesized list.</p>
<p>func MySqrt(f float) (float, bool) {</p>
<p>if f &gt;= 0 { return math.Sqrt(f), true }</p>
<p>return 0, false</p>
<p>}</p>
<p>Functions with result variables</p>
<p>The result &#8220;parameters&#8221; are actual variables you can use if you name them.</p>
<p>func MySqrt(f float) (v float, ok bool) {</p>
<p>if f &gt;= 0 { v,ok = math.Sqrt(f),true }</p>
<p>else { v,ok = 0,false }</p>
<p>return v,ok</p>
<p>}</p>
<p>The result variables are initialized to &#8220;zero&#8221;</p>
<p>(0,0.0, false etc. according to type; more in a sec).</p>
<p>func MySqrt(f float) (v float, ok bool) {</p>
<p>if f &gt;= 0 { v,ok = math.Sqrt(f),true }</p>
<p>return v,ok</p>
<p>}</p>
<p>The empty return</p>
<p>Finally, a return with no expressions returns the existing value of the result variables. Two more versions of MySqrt:</p>
<p>func MySqrt(f float) (v float, ok bool) {</p>
<p>if f &gt;= 0 { v,ok = math.Sqrt(f),true }</p>
<p>return // must be explicit</p>
<p>}</p>
<p>func MySqrt(f float) (v float, ok bool) {</p>
<p>if f &lt; 0 { return } // error case</p>
<p>return math.Sqrt(f),true</p>
<p>}</p>
<p>Function literals</p>
<p>As in C, functions can&#8217;t be declared inside functions -but function literals can be assigned to variables.</p>
<p>func f() {</p>
<p>for i := 0; i &lt; 10; i++ {</p>
<p>g := func(i int) { fmt.Printf(&#8221;%d&#8221;,i) };</p>
<p>g(i);</p>
<p>}</p>
<p>}</p>
<p>Function literals are closures</p>
<p>Function literals are indeed closures.</p>
<p>func adder() (func(int) int) {</p>
<p>var x int;</p>
<p>return func(delta int) int {</p>
<p>x += delta;</p>
<p>return x</p>
<p>}</p>
<p>}</p>
<p>var f = adder();</p>
<p>fmt.Print(f(1));</p>
<p>fmt.Print(f(20));</p>
<p>fmt.Print(f(300));</p>
<p>Prints 1 21 321 &#8211; accumulating in f&#8217;s x</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.go-program.com/go-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
