Lesson 1
Recall Functions in C++
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:

C++
1int add(int, int); // function declaration 2 3int add(int a, int b) { // function definition 4 return a + b; 5} 6 7int main() {return 0;}

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:

C++
1int add(int a, int b) { 2 return a + b; 3} 4 5double add(double a, double b) { 6 return a + b; 7} 8 9int main() {return 0;}

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():

C++
1#include <iostream> 2 3int add(int a, int b) { 4 return a + b; 5} 6 7double add(double a, double b) { 8 return a + b; 9} 10 11int main() { 12 int sum_ints = add(2, 3); 13 double sum_doubles = add(2.5, 3.5); 14 15 std::cout << "Sum of ints: " << sum_ints << '\n'; // Sum of ints: 5 16 std::cout << "Sum of doubles: " << sum_doubles << '\n'; // Sum of doubles: 6.0 17 18 return 0; 19}

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:

C++
1double find_average(int* array, int size) { // function definition 2 if (size == 0) { 3 return 0; // avoid division by zero 4 } 5 int sum = 0; 6 for (int i = 0; i < size; ++i) { 7 sum += array[i]; 8 } 9 return (double)sum / size; 10} 11 12int main() {return 0;}

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:

C++
1#include <iostream> 2 3double find_average(int* array, int size) { // function definition 4 if (size == 0) { 5 return 0; // avoid division by zero 6 } 7 int sum = 0; 8 for (int i = 0; i < size; ++i) { 9 sum += array[i]; 10 } 11 return (double)sum / size; 12} 13 14int main() { 15 int numbers[] = {1, 2, 3, 4, 5}; 16 int size = sizeof(numbers) / sizeof(numbers[0]); 17 18 double average = find_average(numbers, size); 19 20 std::cout << "The average is: " << average << '\n'; // The average is: 3 21 22 return 0; 23}

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:

C++
1#include <cmath> 2 3bool is_prime(int num) { // function definition 4 if (num <= 1) { 5 return false; 6 } 7 for (int i = 2; i <= sqrt(num); ++i) { 8 if (num % i == 0) { 9 return false; 10 } 11 } 12 return true; 13} 14 15int main() {return 0;}

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:

C++
1#include <cmath> 2#include <iostream> 3 4bool is_prime(int num) { // function definition 5 if (num <= 1) { 6 return false; 7 } 8 for (int i = 2; i <= sqrt(num); ++i) { 9 if (num % i == 0) { 10 return false; 11 } 12 } 13 return true; 14} 15 16int main() { 17 int number = 29; 18 19 if (is_prime(number)) { 20 std::cout << number << " is a prime number." << '\n'; // 29 is a prime number. 21 } else { 22 std::cout << number << " is not a prime number." << '\n'; 23 } 24 25 return 0; 26}

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.