<?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>Talk Binary &#187; void</title>
	<atom:link href="http://talkbinary.com/tag/void/feed/" rel="self" type="application/rss+xml" />
	<link>http://talkbinary.com</link>
	<description>Programming Resources, Technology, Computers</description>
	<lastBuildDate>Mon, 06 Feb 2012 17:01:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Introduction to Functions</title>
		<link>http://talkbinary.com/programming/c/functions-in-c/</link>
		<comments>http://talkbinary.com/programming/c/functions-in-c/#comments</comments>
		<pubDate>Fri, 25 Jul 2008 19:51:48 +0000</pubDate>
		<dc:creator>Diego</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[return type]]></category>
		<category><![CDATA[void]]></category>

		<guid isPermaLink="false">http://talkbinary.com/?p=392</guid>
		<description><![CDATA[Functions allow you to perform the same operation on many inputs with one simple call. Why a function anyways? First of all, let&#8217;s see how we&#8217;d perform a certain task without one. How would we print out the entire contents of a vector each time we modified it? Let&#8217;s show how we would do it<a class="moretag" href="http://talkbinary.com/programming/c/functions-in-c/">&#160;&#160;Full Article&#8230;</a>
]]></description>
			<content:encoded><![CDATA[<p>Functions allow you to perform the same operation on many inputs with one simple call. </p>
<h3 id="section-1">Why a function anyways?</h3>
<p>First of all, let&#8217;s see how we&#8217;d perform a certain task without one. How would we print out the entire contents of a vector each time we modified it? Let&#8217;s show how we would do it once. </p>
<pre lang="c++">vector <int> vec;
//After populating our vector...
for ( int i = 0; i < vec.size(); i++ ) {
cout << vec[i] << " "; } </pre>
<p>Are we really going to write this code every time we want to print out the contents? We don't have to. We can create a function that executes several instructions based on given inputs and produces an output. (Note: This isn't always the case) Example? Place car parts onto an assembly line, and the assembly line produces a car! Same with a function. </p>
<h3 id="section-2">The function</h3>
<p>Below is the syntax:</p>
<pre lang="c++">
return_type name(parameters) {
     statements
     return return_type;
}</pre>
<p>You can return data types such as strings, int, double, bool, and void (simply doesn't return anything).<br />
<span id="more-392"></span><br />
So how would we make our printing of a vector a function? Quite simple. </p>
<pre lang="c++"> void print(vector <int> vec) {
     for ( int i = 0; i < vec.size(); i++ ) {
          cout << vec[i] << " " ; }
     return;
}</pre>
<p>Pretty simple right? Where do we place this function? I'll show you below. </p>
<pre lang="c++">#include <iostream>
#include <vector>
using namespace std;

void print(vector <int> vec) {
     for ( int i = 0; i < vec.size(); i++ ) {
          cout << vec[i] << " " ; }
     return;
}

int main() { 

vector <int> v;
//Populate our vector
print(v); //This is where our function is called

return 0;
} </pre>
<p><b>Question:</b> But wait? Our function does not provide the same name as the one used in our program!<br />
<b>Answer:</b>No worries. The function only needs to know what type of data type you'll be passing in, and it will use it accordingly to its parameters. </p>
<h3 id="section-3">More examples</h3>
<p>Let's use another return type and with more parameters.</p>
<pre lang="c++">int sumofvec(vector <int> vec, int num) {
     int sum = 0;
     for ( int i = 0; i < num || i < vec.size(); i++ )
          sum += vec[i];
     return sum;
}</pre>
<p>This is how we would call it in our main using a populated vector. </p>
<pre lang="c++">cout << "Sum of first 3 numbers in our vector[" << sumofvec(vec,5) << "]\n";</pre>
<p>This would simply call our function on a vector and print out the sum of the first five numbers. If the vector doesn't have five elements, it will stop either way because of my condition.</p>
<h3 id="section-4">Exercises with Functions</h3>
<p>So what now? Try doing the following challenges.</p>
<p><b>Challenge 1:</b> Search for a certain number in a vector. If found return <b>true</b>.<br />
<b>Challenge 2:</b> Return a <b>vector</b> that has the reversed order of elements of the one passed in. </p>
<h3 id="section-5">What now?</h3>
<p>Our next tutorial on functions should show you how to use functions more efficiently and how to do more with them.</p>
]]></content:encoded>
			<wfw:commentRss>http://talkbinary.com/programming/c/functions-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: talkbinary.com @ 2012-02-11 12:32:01 -->
