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

Course Content

C++ Introduction

C++ Introduction

1. Getting Started
2. Introduction to Operators
3. Variables and Data Types
4. Introduction to Program Flow
5. Introduction to Functions

book
What are Functions?

Functions are fundamental building blocks in programming. They are reusable blocks of code designed to perform a specific task. Functions help make code more organized, readable, and easier to maintain. By using functions, you can break a large, complex program into smaller, manageable subroutines.

cpp

main

copy
1234
int main() // `main` is the name of a function { return 0; }

Note

The name main is already reserved by the C++ language. Therefore, when declaring a function with this name, the compiler will generate an error.

Creating a function involves several key steps to ensure it performs a specific task and integrates seamlessly into your program. A function consists of a return type, a name, parameters (if needed), and a body where the logic resides.

Define the typeSpecify the type of data the function will return (e.g., int, void, etc.).
Assign a nameGive the function a meaningful name to identify it.
Provide a bodyWrite a block of instructions within curly braces {...} to define its functionality.
h

get_bank_name

copy
12345678
// Function to return the name of the bank std::string get_bank_name() // type and name of function { // Beginning of the function body std::string bank_name = "Future Savings Bank"; return bank_name; // Return the name of the bank // End of the function body }

After creating a function, the next step is to call it. Calling a function executes the code inside it and allows you to use its result (if it returns a value).

cpp

main

copy
1234567891011121314
#include <iostream> #include <string> // Function to return the name of the bank std::string get_bank_name() { std::string bank_name = "Future Savings Bank"; return bank_name; // Return the name of the bank } int main() { std::cout << "Name of the bank: " << get_bank_name() << std::endl; }

Converting currencies is a common real-life task, especially in global transactions or travel. By creating a function, we can simplify this process, making the conversion reusable and efficient.

cpp

main

copy
123456789101112131415
#include <iostream> // Function to convert USD to Euros double convert_usd_to_eur(double usd_amount) { const double exchange_rate = 0.91; double euros = usd_amount * exchange_rate; return euros; } int main() { double usd = 100.0; // Amount in USD std::cout << usd << " USD = " << convertUsdToEur(usd) << " EUR" << std::endl; }
h

function

copy
123
int add_numbers(int a, int b); // 'a' and 'b' are parameters add_numbers(5, 10); // 5 and 10 are arguments passed to the function

In programming, arguments are values or variables that you pass to a function when calling it. These values provide the input the function needs to perform its task. By passing arguments, you can make functions more dynamic and reusable.

Function ParametersPlaceholders defined in the function header that specify the type and name of the data the function expects.
ArgumentsThe actual values or variables passed to a function when calling it, which are assigned to the corresponding parameters.
Passing Values to VariablesDuring a function call, arguments are assigned to the parameters. Inside the function, these parameters behave like regular variables.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 1
We're sorry to hear that something went wrong. What happened?
some-alt