Functions

When I was at university I had a mental block when it came to functions. I’m slowly getting used to them.

They are important. We take them for granted, especially in a language such as Python, every line I wrote included a built in function. It’s a good idea to use functions, it’s more efficient and when it comes to the next time you want to use a similar method, you already have your template.

I’m going to keep this brief, as I’m not 100% confident with functions yet. Here’s an example of an Addition Function:


#include <iostream>

using namespace std;

/* This function takes in two arguments a and b.

Then calculates the sum and return the answer */

int addition(int a, int b)
{
 int r;
 r = a + b;
 return r;
}

int main()
{
 int z;
 z = addition(5,3);             // Calling the Addition function
 cout << "The result is " << z;
}

Just a note, the addition function is type int because it returns an integer. A function with type void has no return value.

Advertisements

One thought on “Functions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s