Conteúdo do Curso
C++ Functions
C++ Functions
2. Function Arguments Specification
3. Function Return Values Specification
4. Some Advanced Topics
What is Function in C++?
In C++, a function is a block of code that performs a specific task. You're familiar with this concept and have used it with the main()
function, which is special in C++ as it marks the starting point for a program. However, using functions in C++ is not limited to just the main()
function. Here's an example:
main
#include <iostream> // Function to calculate the factorial of an integer int calculateFactorial(int n) { int factorial = 1; for (int i = 1; i <= n; i++) factorial *= i; return factorial; } int main() { // Call the calculateFactorial function and print the result std::cout << calculateFactorial(5) << std::endl; std::cout << calculateFactorial(8) << std::endl; }
Let's clarify what was done in the code above:
- We created
calculateFactorial()
function that calculates the factorial of the given number and returns it as an output. - We called this function in our program's
main()
function and printed the result into the console.
You might not get what happened yet – like how we made the function, what stuff we put into it, and what's inside the code. But don't worry, we'll explain it all soon.
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 1. Capítulo 1