[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)

[/spoiler]

[spoiler title=”Lesson Source Code”]

```#include
#include

using namespace std;

int main(){

//Homework -- Create a function that allows 2 variables to be set to it, multiply them together, output the result, return the outcome to main, add 5 and redisplay the outcome.
int a = 5, b = 7;

cout << "The total of " << a << " + " << b << " = " << addNumbers(a, b);

cout << endl;
system("PAUSE");
return 0;
}

//int -- return type -- return goes back to where it was called from
//addNumbers -- Name of our function
// () --Arguments
// (int x, int y) -- argument list
//{  } -- Scoping brackets

return x + y;
}
```

[/spoiler]

Homework: Create a function that allows 2 variables to be set to it, multiply them together, output the result, return the outcome to main, add 5 and redisplay the outcome.

What is a function
A function is a section of code that can be called independently from anywhere else in your program to perform a specific task or group of tasks. Functions are able to be passed variables, called "arguments" that are able to be used in the function. These variables are usually passed by "value" (meaning a copy of the variable is made and passed to the function) and in certain cases are passed by reference (meaning the item of the argument itself is passed). Pass by referent will be touched on next lesson, and later on in the series.

Function declarations are broken into 3 parts. The return type, the name, and the argument list.

The return type also dictates another piece of code. If you specify anything other than void (return nothing) as a return type, then you need to return a variable / constant of that type.

For example, the following are all valid examples of returning an int.

```//Assume we need to return an int, and x is an int.
return x;
return x+5
return 5;
return (2*4)*x;
```

A few example functions and how you could call them

```//Example functions
string returnNumberAsWord (int x){
if ( x == 1) return "one";
if ( x == 2) return "two";
if ( x == 3) return "three";
return "Your number was too high for this function"; //This will only get hit if the 3 before it didn't happen
}

char numberToletter( int x){
if (x == 1) return 'a';
if (x == 2) return 'b';
if (x == 3) return 'c';
...
if (x == 26) return 'z';
return ' '; // Will return a space if none of the above match.
}

return x+5;
}

...

int main(){
string word = returnNumberAsWord(3); // Three
char someLetter = numberToLetter(3); // c
{"ft_lang_label":"__label__en","ft_lang_prob":0.8279539,"math_prob":0.95624816,"size":3613,"snap":"2019-51-2020-05","text_gpt3_token_len":936,"char_repetition_ratio":0.13965087,"word_repetition_ratio":0.08011869,"special_character_ratio":0.28757265,"punctuation_ratio":0.119382024,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98525,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-01-19T15:43:10Z\",\"WARC-Record-ID\":\"<urn:uuid:195acdd9-1d4b-4499-a654-f7cdd514118b>\",\"Content-Length\":\"35706\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a9c69e58-3f8f-4f3a-9276-58aefd39caaa>\",\"WARC-Concurrent-To\":\"<urn:uuid:f16183be-b0d5-44ce-9dda-d90060a82b4a>\",\"WARC-IP-Address\":\"68.183.148.96\",\"WARC-Target-URI\":\"https://beginnerscpp.com/lesson-18-introduction-to-functions/\",\"WARC-Payload-Digest\":\"sha1:3FDC76RWYJZXRQNO2EAMDA5M4TLHOWKV\",\"WARC-Block-Digest\":\"sha1:M65FRF3Z53EXMGAYNEA46IIIF5NW7KPD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-05/CC-MAIN-2020-05_segments_1579250594662.6_warc_CC-MAIN-20200119151736-20200119175736-00014.warc.gz\"}"}