# 10.1: Functions - Declarations and Definitions

$$\newcommand{\vecs}{\overset { \rightharpoonup} {\mathbf{#1}} }$$ $$\newcommand{\vecd}{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}}$$$$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\id}{\mathrm{id}}$$ $$\newcommand{\Span}{\mathrm{span}}$$ $$\newcommand{\kernel}{\mathrm{null}\,}$$ $$\newcommand{\range}{\mathrm{range}\,}$$ $$\newcommand{\RealPart}{\mathrm{Re}}$$ $$\newcommand{\ImaginaryPart}{\mathrm{Im}}$$ $$\newcommand{\Argument}{\mathrm{Arg}}$$ $$\newcommand{\norm}{\| #1 \|}$$ $$\newcommand{\inner}{\langle #1, #2 \rangle}$$ $$\newcommand{\Span}{\mathrm{span}}$$$$\newcommand{\AA}{\unicode[.8,0]{x212B}}$$

# Functions in C++

A function is a set of statements that take zero or more arguments, does some specific computation and returns some value. All C++ programs have at least one function - that is the main() function.

The idea is to put code that is repeated multiple times into a function so that instead of writing the same code again and again for different inputs, we can call the function.

Why do we need functions?

• Functions help us in reducing code redundancy. If the same functionality is necessary at multiple places in the project, then rather than writing the same code, again and again, we create a function and call it whenever needed. This also helps with code maintenance as we only have to change the code in one place and that change is good for every time the function gets called.
• Consider a big project with thousands of lines of code. Using functions helps to reduce the number of lines of code, makes the code easier to read and make code maintenance easier.
• Consider library functions, we can call them without worrying about how they work, we can simply call the function, pass any necessary arguments and accept the returned results.

There are two tasks that are required when coding functions: provide a function prototype and provide the function definition.

## Prototypes

The function prototype only contains the declaration of the function. For instance take the following example of a prototype for a function named Sum().

int Sum( int, int );


First of all, function prototypes include the function signature, which includes, the name of the function, return type and argument number and type In this case the name of the function is "Sum". The function signature determines the number of parameters, in this instance two, and their types, both are int values. In the above example, the return type is "int", meaning the function returns an integer value back to the calling function. Notice that in the function prototype you are NOT required to provide variable names, just the types of the arguments is required. The purpose of the prototype is to tell the compiler that there is a function named, in our example "Sum", and it accepts, in this example 2 integer arguments, and returns a value, in our example a "void" value, meaning we don't pay attention to the return value.

Any number of arguments (also called parameters) can be passed, and you can mix any of the valid types in the list.

int myFunction(float, char, int);


## Function Definition

Since the prototype does not contain any actual code, we must also prvide the function definition, which is where the code is that gets executed when the function is called. Functions should do something specific, and then return to the calling location in the code. Your functions should not add a bunch of other processing that is not part of the specific processing. This takes practice to know when to return to the calling code and continue the processing there.

The following is a simple example and includes the prototype and the definition, as well as a function call from main():

#include <iostream>
using namespace std;

// Prototype is here, BEFORE it is used
// This is the function declaration
void output(char);

int main()
{
char newCh = 'A';

output(newCh);

return 0;
}

// Function definition - includes the code for
// the function
void output(char inCh)
{
cout << "The value is " << inCh << endl;
return;
}

Some of this code looks familiar. On line 6 we have the function prototype - simply tells the compile that there is a function called output, takes a single character argument, and return no value -  it is type void. There is no code for this prototype yet.

In main() we declare a character variable named newCh and assign it the value of 'A'. Then we actually make the function call, passing the proper type of argument - a character.

At the bottom of our code we have the function definition, where the code for this function is provided. Now, on the first line of the function we have a variable type AND a variable name - inCh. A WORD OF WARNING - do not use the same variable name here that you used when you called the function - for instance do NOT call both of your variables newCh or inCh. While this is actually allowed in C++ it will generate confusion and in this class you will lose points for coding in that manner. Also - notice the return statement does not actually return a value - because this is a void function. If you put a value in the return statement you will get a compile error.

When the function returns, control of the program returns the the line of code in main() where the function was called, and execution continues from that point.

Definitions

User Defined Library
A file containing specific task functions created by individuals to be used in many programs.