
Free course: “Quick start in Python»
Learn MoreThis is the fifth article in the "Deep Dive into C++" series. In the previous article, we looked at data entry methods and exception handling. In this article, we will focus on functions and procedures, their definition and use in C++. Functions play a key role in structuring your code, allowing you to break down problems into smaller, more manageable parts. We will discuss how to properly declare functions, pass parameters, and return values. We will also look at the importance of procedures in the context of code organization and optimization.
If code needs to be used multiple times, it is recommended to factor it out into a subroutine, such as a function or procedure. This will significantly reduce the code size and make it easier to maintain. Consider the following example program:
The program greets six users sequentially. While this is fairly simple code, the question arises: what if you want to change the phrase "Hello, %name%!" to "Hello, %name%! How are you?"? In this case, you would have to edit the code in six different places. If such greetings need to be displayed a hundred or even a thousand times, this becomes extremely inconvenient. Therefore, it is important to apply more efficient methods, such as using functions or templates, which allow you to centrally manage messages and simplify the process of making changes. This approach not only saves time but also minimizes the likelihood of errors.
Subroutines are essential for preventing various programming problems. Their use allows you to break complex tasks into simpler and more manageable parts, which significantly simplifies the development process. Thanks to subroutines, the code becomes more structured, it is easier to read and maintain. In addition, they promote code reuse, which saves developers time and resources. It is important to keep in mind that properly organized subroutines can significantly improve the efficiency of a team and the quality of the final product.
How to create a function in C++
For convenience, all subroutines, including procedures, are usually called functions. This makes it easier to understand and use the program code, and also improves its readability. Using the term "functions" creates consistency in notation, which is especially important when developing complex software systems.
We've already created one function—main(). It serves as the entry point for each program. The remaining functions are created in a similar manner. It's important to remember that proper function structure helps organize code, improve its readability, and improve reuse. Creating functions in programming simplifies the development process and allows you to effectively solve problems by breaking them down into smaller subtasks.

Sorry, you have not provided the text that needs editing. Please paste the text, and I will help you with its processing and optimization for SEO.
Functions in programming can accept arguments or work without them. If you've defined a function as requiring arguments, calling it without specifying the required parameters will result in an error. This is important to consider during development to avoid crashes and ensure correct code execution.
When specifying the return type in a function, you must always use the return statement. If no value is required to be returned, you should create a procedure and specify the void type. This will make the code structure clearer and more optimized. Proper use of the return statement and the void type improves the readability and maintainability of code.
The program will produce the following output:

The main() function in the C programming language automatically returns a value, which makes the return statement mandatory. If the program is executed successfully, it will return 0, otherwise, another value will be returned indicating that an error occurred. This behavior allows you to effectively track the results of program execution and handle possible exceptions.
Scopes in C++
Functions in programming have the property of isolation. This means that they can interact only with the arguments that were passed when they were called. Thus, all operations and calculations performed within the function remain local and do not affect external variables unless they were explicitly passed as arguments. This property makes functions convenient for organizing code and managing the scope of variables. Using functions allows you to create more structured and cleaner code, which makes it easier to read and maintain.
Variables created inside a code block enclosed in curly braces {} have a scope limited to this block and all its nested blocks. These variables are classified as local. Local variables cannot be used outside their block, which helps avoid name conflicts and ensures cleaner code structure.
If you want a variable to be accessible throughout your code, it must be declared outside of any blocks. Such variables are known as globals. Global variables provide convenient access to data across functions and modules, which can significantly simplify the development and maintenance of your software. However, it's important to remember that overusing global variables can lead to confusion and errors, so their use should be judicious and considered.
Using global variables in programming is not recommended because it can make it difficult to track changes and manage the program's output. Global variables can be difficult to debug and increase the likelihood of errors. The exception is constants, which are best defined as global because they remain unchanged throughout the program's execution. Complex systems are best designed using local variables and a clear structure to improve the readability and maintainability of the code.
How Arguments Work
When you pass an argument to a function, several key processes occur. Consider the following code example:
You might expect the program to return 505, but this is not the case. Functions take the variables' values as arguments, not the variables themselves. So, the sum() function is not passed a memory location, but the specific number 5 that was previously stored in that location. Please note this when writing programs. Understanding this aspect will help you avoid mistakes and improve the quality of your code.

This aspect applies exclusively to primitive data types. Reference data types function differently and have their own unique characteristics. Primitive types represent basic values that have no methods or properties, while reference types contain references to objects in memory, which affects their behavior and interactions. Understanding the differences between primitive and reference data types is key to effective programming.
C++ Recursion
Recursion is the concept of an object containing itself, which should not be confused with fractals. In the context of programming, recursion manifests itself in the unique property of functions that can call themselves. This technique allows you to solve complex problems by breaking them down into simpler subproblems, making code more elegant and understandable. Recursive functions are used in various algorithms, such as sorting, searching, and processing data structures. Understanding recursion is an important aspect for developers looking to write efficient and optimized code.

Recursion is a programming technique in which a function calls itself to solve a problem. This concept allows complex problems to be broken down into simpler subproblems, making code more compact and understandable. To gain a deeper understanding of recursion, it's important to understand its basic principles and application examples. Recursion is widely used in algorithms such as tree traversal and solving problems involving mathematical sequences. Understanding recursion will open new horizons in software development and algorithm optimization.
Recursion is often explained using factorials as an example, which should not be confused with fractals. The factorial of n, denoted as n!, is the product of all positive integers from 1 to n. The recursive definition of a factorial is that the factorial of zero is one, and the factorial of any positive number n is n multiplied by the factorial (n-1). This approach clearly demonstrates the principles of recursion, allowing a problem to be solved through repeated calls to the function itself. Recursion is a powerful tool in programming and mathematics, allowing complex problems to be effectively solved by breaking them down into simpler subproblems.
Factorials of numbers from 1 to 10 are an important mathematical concept. The factorial of n, denoted as n!, is defined as the product of all natural numbers from 1 to n. For numbers from 1 to 10, the factors are as follows:
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Factorials are widely used in combinatorics, probability theory and various areas of mathematics. Understanding and calculating factorials can be useful for solving problems involving permutations and combinations.

When writing recursive functions, it is necessary to clearly define the exit conditions for the recursion. Without such conditions, the program may get into a loop, which will lead to a significant load on the processor and may even cause the system to freeze. Proper recursion management avoids excessive resource consumption and ensures stable application operation.
In practice, the use of this feature becomes obvious when recursively deleting folders. The folder deletion function should be implemented as follows: it should first check for nested files and subfolders, and then sequentially delete them before deleting the folder itself. This approach ensures that all items within the folder are correctly processed and deleted, preventing possible errors and maintaining a clean file system. The recursive algorithm allows you to effectively cope with the deletion task, minimizing the number of necessary operations and optimizing the process.

