Lesson 2
Mastering the Basics of Functions in C++
Introduction to Functions in C++

Welcome to our lesson on C++ functions! Functions form the building blocks of any program by encapsulating chunks of code into reusable modules, which are key to constructing complex code. Let's explore further!

Understanding Functions

Have you ever used a recipe? If so, you've employed a real-world function, albeit an ad hoc one. A function is a set of instructions designed to accomplish a specific task. Functions help package blocks of code into reusable components, a concept known as code reusability.

Function Syntax and Definition

Every C++ function consists of a name, a return type, and a parameter list, forming the following syntax:

C++
1return_type function_name( parameter list ) { 2 // function body 3}

Let's break down each part.

Function: Example 1

Let's consider a function without arguments that prints a greeting in the console. It doesn't take any parameters, and a void return type specifies it doesn't return a value.

Example function:

C++
1void greet() { 2 std::cout << "Hello, C++ learner!" << std::endl; 3}

Example function call:

C++
1#include <iostream> 2 3int main() { 4 greet(); // Hello, C++ learner! 5 return 0; 6}

While this function might seem not very useful, it already brings out the most significant advantage of using function: reusability! We can call this function as many times as we want without rewriting its code:

C++
1#include <iostream> 2 3int main() { 4 greet(); // Hello, C++ learner! 5 greet(); // Hello, C++ learner! 6 greet(); // Hello, C++ learner! 7 return 0; 8}
Function: Example 2

Let's consider a function with arguments, which can accept parameters, It will still have a void return type, meaning it doesn't return a value.

Example function:

C++
1void greet(string name) { 2 std::cout << "Hello, " << name << "!" << std::endl; 3}

This function prints out a personalized greeting, using the provided name. Here is an example code:

C++
1#include <iostream> 2#include <string> 3 4int main() { 5 greet("Bob"); // Hello, Bob! 6 greet("Alice"); // Hello, Alice! 7 greet("C++ learner"); // Hello, C++ learner! 8 return 0; 9}

Now, our program is more friendly!

Function: Example 3

Let's consider a function with multiple parameters:

C++
1void printSum(int x, int y) { 2 int sum = x + y; 3 std::cout << "The sum is: " << sum << std::endl; 4}

This function takes two numbers as arguments. It calculates and prints their sum.

Example function call:

C++
1#include <iostream> 2 3int main() { 4 printSum(5, 6); // The sum is: 11 5 printSum(3, 4); // The sum is: 7 6 return 0; 7}
Function: Example 4

While printing the sum this easy is awesome, it is not the optimal design for your code. A function is supposed to be as general as possible: we must be able to reuse it in different scenarios. With that said, it is more common to define a function that both accepts arguments and returns a value.

Example function:

C++
1int addNumbers(int x, int y) { 2 int sum = x + y; 3 return sum; 4}

Here is how we call it:

C++
1#include <iostream> 2 3int main() { 4 int result = addNumbers(5, 6); 5 std::cout << "The sum is: " << result; // The sum is: 11 6 return 0; 7}

Note that we still print the sum, but not directly in the function. We first put the result into a variable and then print it. The advantage of it is that we can now use the function in cases where we don't need to print the sum. For example:

C++
1#include <iostream> 2 3int main() { 4 int result = addNumbers(5, 6) * 3; 5 std::cout << "The result is: " << result; // The result is: 33 6 return 0; 7}

Here, we use the result of our function as an intermediate step for some calculation.

Lesson Summary and Practice

Today, we've learned the basics, components, and applications of functions. We've understood how to declare, define, and call a function, and how to understand function arguments and return values. Practice makes perfect, so prepare for hands-on exercises to sharpen your programming skills. Keep coding!

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