Lesson Introduction

Welcome to the lesson on recalling functions in C++. Functions are fundamental building blocks that allow for code modularity, reusability, and better organization. Understanding functions helps you write cleaner and more maintainable code.

The goal of this lesson is to refresh your memory on defining and declaring functions, understanding function overloading, and learning to call these functions effectively.

Functions: Declaration and Definition

In C++, a function must be declared before it is used. This informs the compiler about the function's name, types of inputs, and output. Function declaration is like making a promise that the actual function (function definition) will appear in the code. However, if the function is defined before it is used, there is no need for a separate declaration. The compiler will already know about the function when it encounters the function call.

Function definition provides the function's actual body. It's where the promised function does its work.

Consider these declarations and definitions in our code snippet:

The add functions return the sum of the two input parameters.

Function Overloading

C++ allows function overloading, meaning you can have multiple functions with the same name but different parameter lists. The compiler differentiates them based on the number and types of parameters.

Function overloading is useful when actions are conceptually the same but require different types or numbers of inputs. For example, you might want to add integers sometimes and doubles at other times. Overloading allows you to use the same function name (add) while handling both types.

Here’s our overloaded add function:

This shows two versions of add: one for integers and one for doubles. The compiler decides which version to call based on the provided argument types.

Using Functions in `main()`

Once functions are declared and defined, they can be called from the main() function or any other function.

Calling a function involves specifying the function name followed by arguments in parentheses.

In our code snippet, we call the add function from main():

Here, add(2, 3) calls the integer version of add, returning 5. Similarly, add(2.5, 3.5) calls the double version, returning 6.0. The results are then printed using std::cout.

Advanced Functions Example: part 1

Let's look at an example that involves working with arrays. Suppose we want a function that calculates the average of an array of integers. We'll call this function find_average. The function will take a pointer to an array and the array size, iterate through the elements to find the sum, and then compute the average.

Here's how to implement find_average:

Note that we cast sum to double before the division to avoid integer division, which will round the value.

In the main() function, you can call find_average like this:

In this example, the find_average function calculates the average of the integers in the numbers array and returns the result.

Advanced Functions Example: part 2

Next, we'll recall boolean functions by implementing a function to check if a number is prime. A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. We'll call this function is_prime.

Here's how to implement is_prime:

Explanation:

  • If the number is less than or equal to 1, it's not prime.
  • The function checks for divisors up to square root of num.
  • If a divisor is found, false is returned.
  • If no divisors are found, true is returned.

In the main() function, you can call is_prime like this:

In this example, the is_prime function checks if number is prime and returns true if it is, and false otherwise. The result is printed using std::cout.

Lesson Summary

In this lesson, we reviewed function declaration, definition, and overloading in C++. We saw how to declare a function to inform the compiler of its existence, define the function to specify its behavior, and overload functions to handle different data types and parameters.

It's time to put what you've learned into practice. By working through practical exercises, you'll deepen your understanding and make these concepts second nature.

Let's move on to the practice session, where you'll write and call your functions, reinforce overloading concepts, and ensure you've mastered the basics of C++ functions!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal