Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What is Function in C++? | Introduction
C++ Functions
course content

Зміст курсу

C++ Functions

C++ Functions

1. Introduction
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:

cpp

main

copy
1234567891011121314151617
#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:

  1. We created calculateFactorial() function that calculates the factorial of the given number and returns it as an output.
  2. 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.

Are you ready to start learning functions?

Виберіть правильну відповідь

Все було зрозуміло?

Секція 1. Розділ 1
We're sorry to hear that something went wrong. What happened?
some-alt