Learning C++: Part 3: Functions

Learning C++


Functions

In programming terminology, a function is a set of instructions. Functions are a way of abstracting parts of code into sections that can do specialized or repetitive tasks. They are a way of writing code that can be called to do smaller tasks or as part of some larger process.

Code execution starts in C++ from the main() function. Like all functions in C++, it is composed of five parts: its return type, name, parameters, instructions, and a return statement.

Return Type

Like variables, functions in C++ also have types. The return type of a function is what type of data it will return.

One way to think of function is like a process where input is given and it results in some output. The type of output we can expect from the function is its return type. When defining functions, the return type is the first entry.

Return types must match existing data types. Often, these will be something like intfloat, or some other type, but they can also other, more complex data types as well.

Name

The name of the function comes after its return type. This is the name the function is known by within the code. In order to use a function, it must be called by using its name.

For the main() example, the name of the function is “main.” To use the function in some other section of code outside of main(), it would be called through using its name and opening and closing parentheses.

Parameters

The entries inside of a function’s open and closing parentheses are its parameters. Thinking back to the conceptualization of a function being a process where input is given and output is taken, the parameters are the input to the function.

Like variables and functions themselves, parameters also have types. The type of the parameters lets C++ know what kind of data will be passed to the function. Each parameter is separated by commas from the next and has a name and type.

The name of the variable will be what it will be called within the function. Using the name of parameters helps with accessing those values within the function itself.

Instructions

The instructions inside of a function can also be thought of as its body. Whatever the function should do, the instructions make up its core. From one line to potentially thousands, whatever the function does is part of its instructions.

In C++, like with many other programming languages, the instructions of a function are contained with open and closing curly brackets. Starting from the end of the parameters and the opening curly bracket, the instructions start and run until the ending, closing curly bracket is found.

Return Statement

In order to get data from a function, some data must be be returned. In C++, this is the return statement. It uses the return keyword.

Whatever follows after the return statement is sent back to whatever called the function.

Functions Can Be Nested

Functions can be called within other functions. This is an easy way to chain functions together and have what is returned from one function used in other functions.

When working with system functions like printf(), this can be a quick way to get data from one function and use it in another.

Play with the example Repl.it!

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.