<?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>C ++ &#187; Function Argument Types</title>
	<atom:link href="http://www.bizzymicbizness.com/cplusplus/tag/function-argument-types/feed" rel="self" type="application/rss+xml" />
	<link>http://www.bizzymicbizness.com/cplusplus</link>
	<description>All about C++</description>
	<lastBuildDate>Mon, 19 Oct 2009 12:38:17 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Passing and Returning Structures</title>
		<link>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures</link>
		<comments>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures#comments</comments>
		<pubDate>Thu, 08 Oct 2009 04:25:44 +0000</pubDate>
		<dc:creator>BMB</dc:creator>
				<category><![CDATA[C++ Glossary]]></category>
		<category><![CDATA[Function]]></category>
		<category><![CDATA[Function Argument Types]]></category>
		<category><![CDATA[Function Return Types]]></category>
		<category><![CDATA[Passing and Returning Structures]]></category>
		<category><![CDATA[struct]]></category>
		<category><![CDATA[Variables]]></category>

		<guid isPermaLink="false">http://www.bizzymicbizness.com/cplusplus/?p=622</guid>
		<description><![CDATA[Passing structures by value makes the most sense when the structure is relatively compact

So let’s look at a couple examples along those lines


The first example deals with travel time (not to be confused with time travel)

Some maps will tell you that

It is 2 hours, 56 minutes, from Zero One City to Binary Falls
And 1 hour, [...]]]></description>
			<content:encoded><![CDATA[<p>Passing structures by value makes the most sense when the structure is relatively compact</p>
<ul>
<li>So let’s look at a couple examples along those lines</li>
</ul>
<p><br class="blank" /><br />
The first example deals with travel time (not to be confused with time travel)</p>
<ul>
<li>Some maps will tell you that
<ul>
<li>It is 2 hours, 56 minutes, from Zero One City to Binary Falls</li>
<li>And 1 hour, 16 minutes, from Binary Falls to Hexadecimal Valley</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
You can use a structure to represent such times</p>
<ul>
<li>Using one member for the hour value</li>
<li>And a second member for the minute value</li>
</ul>
<p><br class="blank" /><br />
Adding two times is a little tricky</p>
<ul>
<li>Because you might have to transfer some of the minutes to the hours part
<ul>
<li>For example, the two preceding times sum to 3 hours, 72 minutes, which should be converted to 4 hours, 12 minutes</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Let’s develop a structure to represent a time value</p>
<ul>
<li>And then a function that takes two such structures as arguments
<ul>
<li>And returns a structure that represents their sum</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Defining the structure is simple (A-1):<br />
<br class="blank" /><br />
Example of (A-1)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">struct</span> travel_time
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> hours<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> mins<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
Next, consider the prototype for a <strong>sum()</strong> function that returns the sum of two such structures</p>
<ul>
<li>The return value should be type <strong>travel_time</strong>, and so should the two arguments
<ul>
<li>Thus, the prototype should look like this (A-2):</li>
</ul>
</li>
</ul>
<p><br class="blank" /><br />
Example of (A-2)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p><br class="blank" /><br />
To add two times, you first add the minute members </p>
<ul>
<li>Integer division by 60 yields the number of hours to carry over</li>
<ul>
<li>And the modulus operation (<strong>%</strong>) yields the number of minutes left</li>
</ul>
</ul>
<p><br class="blank" /><br />
(A-3) incorporates the above approach into the <strong>sum()</strong> function</p>
<ul>
<li>And adds a <strong>show_time()</strong> function
<ul>
<li>To display the contents of a <strong>travel_time</strong> structure</li>
</ul>
</li>
</ul>
<p> <br />
<br class="blank" /><br />
Example of (A-3)</p>

<div class="syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// travel.cpp -- using structures with functions</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">struct</span> travel_time
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> hours<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">int</span> mins<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> Mins_per_hr <span style="color: #000080;">=</span> <span style="color: #0000dd;">60</span><span style="color: #008080;">;</span>
&nbsp;
travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">void</span> show_time<span style="color: #008000;">&#40;</span>travel_time t<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	travel_time day1 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">56</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 2 hrs, 56 min</span>
	travel_time day2 <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">16</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// 1 hrs, 16 min</span>
&nbsp;
	travel_time trip <span style="color: #000080;">=</span> sum<span style="color: #008000;">&#40;</span>day1, day2<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Two-day total: &quot;</span><span style="color: #008080;">;</span>
	show_time<span style="color: #008000;">&#40;</span>trip<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	travel_time day3<span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #0000dd;">5</span>, <span style="color: #0000dd;">12</span><span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Three-day total: &quot;</span><span style="color: #008080;">;</span>
	show_time<span style="color: #008000;">&#40;</span>sum<span style="color: #008000;">&#40;</span>trip, day3<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
travel_time sum<span style="color: #008000;">&#40;</span>travel_time t1, travel_time t2<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	travel_time total<span style="color: #008080;">;</span>
&nbsp;
	total.<span style="color: #007788;">mins</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span>t1.<span style="color: #007788;">mins</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">mins</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">%</span> Mins_per_hr<span style="color: #008080;">;</span>
	total.<span style="color: #007788;">hours</span> <span style="color: #000080;">=</span> t1.<span style="color: #007788;">hours</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">hours</span> <span style="color: #000040;">+</span>
		    <span style="color: #008000;">&#40;</span>t1.<span style="color: #007788;">mins</span> <span style="color: #000040;">+</span> t2.<span style="color: #007788;">mins</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> Mins_per_hr<span style="color: #008080;">;</span>
	<span style="color: #0000ff;">return</span> total<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">void</span> show_time<span style="color: #008000;">&#40;</span>travel_time t<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> t.<span style="color: #007788;">hours</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; hours, &quot;</span>
	     <span style="color: #000080;">&lt;&lt;</span> t.<span style="color: #007788;">mins</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; minutes<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><br class="blank" /><br />
Here <strong>travel_time</strong> acts just like a standard type name; you can use it to</p>
<ul>
<li>Declare variables</li>
<li>Function return types</li>
<li>Function argument types</li>
</ul>
<p><br class="blank" /><br />
Because variables such as <strong>total</strong> and <strong>t1</strong> are <strong>travel_time</strong> structures</p>
<ul>
<li>You can apply the dot membership operator to them</li>
</ul>
<p><br class="blank" /><br />
Note that because the <strong>sum()</strong> function returns a <strong>travel_time</strong> structure</p>
<ul>
<li>You can use it as an argument for the <strong>show_time()</strong> function</li>
</ul>
<p><br class="blank" /><br />
Because C++ functions, by default, pass arguments by value</p>
<ul>
<li>The <strong>show_time(sum(trip, day3))</strong> function call</li>
<ul>
<li>First evaluates the <strong>sum(trip, day3)</strong> function call in order to find its return value</li>
</ul>
<li>The <strong>show_time()</strong> call</li>
<ul>
<li>Then passes <strong>sum()</strong>’s return value, not the function itself to <strong>show_time()</strong></li>
</ul>
</ul>
<p><br class="blank" /><br />
Here’s the output of the program in (A-3):</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-623" title="Output" src="http://www.bizzymicbizness.com/cplusplus/wp-content/uploads/2009/10/Output1.jpg" alt="Output" width="597" height="274" /></p>
<p><br class="blank" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bizzymicbizness.com/cplusplus/passing-and-returning-structures/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

