Key Concepts

Review core concepts you need to learn to master this subject

Return Values in C++ Functions

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

A C++ function which returns a value must have a return statement.

A void function (one that does not return anything) does not require a return statement.

Parameters in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++ function parameters are placeholders for values passed to the function. They act as variables inside a function.

Functions in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++, a function is a set of statements which are executed together when the function is called. Every function has a name, which is used to call the respective function.

Built-in Functions in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

C++ has many built-in functions. In order to use them we have to import the required library using #include.

Calling Functions in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++, when we define a function, it is not executed automatically. To execute it, we need to call the function by specifying it’s name along with ().

void Functions in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++, if we declare the type of a function as void, it does not return a value. These functions are useful for a set of statements that do not require returning a value.

Function Declaration and Definition in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

A C++ function has two parts:

  • Function declaration
  • Function definition

The declaration includes the function’s name, return type, and any parameters.

The definition is the actual body of the function which executes when a function is called. The body of a function is typically enclosed in curly braces.

Function Arguments in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++, when we pass values to a function they are known as arguments. They represent the actual input values.

Scope of Code in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++, the scope is the region of code that can access or view a given element:

  • Variables defined in global scope are accessible throughout the program.
  • Variables defined in a function have local scope and are only accessible inside the function.

Function Declarations in Header file

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

C++ functions typically have two parts: declaration and definition.

Function declarations are generally stored in a header file (.hpp or .h) and function definitions (body of the function that defines how it is implemented) are written in the .cpp file.

Function Template in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

A function template is a C++ tool that allows programmers to add data types as parameters, enabling a function to behave the same with different types of parameters. The use of function templates and template parameters is a great C++ resource to produce a cleaner code, as it prevents function duplication.

Default arguments in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++, default arguments can be added to function declarations so that it is possible to call the function without including those arguments. If those arguments are included the default value is overwritten. Function parameters are read from left to right, so default parameters should be placed from right to left.

C++ Functions Definitions

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++, it is common to store function definitions in a separate .cpp file from the main() function. This separation results in a more efficient implementation. If the file containing the main() function needs to be recompiled, it is not necessary to recompile the files containing the function definitions.

C++ function overloading

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

In C++, function overloading enables functions to handle different types of input and return different types. function overloading allows multiple definitions for the same function name, but all of these definitions must differ in their arguments.

Inline Functions in C++

#include <iostream> int sum(int a, int b); int main() { int a = 10; int b = 20; int r = sum(10, 20); std::cout << r; } int sum(int a, int b) { return(a + b); }

An inline function is a function definition, usually in a header file, qualified by the inline keyword, which advises the compiler to insert the function’s body where the function call is. If a modification is made in an inline function, it would require all files containing a call to that function to be recompiled.

  1. 1
    As a programmer, you will find yourself reusing the same blocks of code over and over throughout your program. In times like these, you can turn to functions. Also known as a method or procedure, …
  2. 2
    Before we learn how to create functions, let’s go over some built-in functions… C++ comes chock-full of functions that are already created as part of the standard library. But how do we access t…
  3. 3
    Often, built-in functions aren’t enough to tackle the wide array of programming challenges out there. But never fear: you can write your own functions too! A C++ function is comprised of two disti…
  4. 4
    Let’s build a simple function with no input and no output. We can do that? Enter the void specifier, which is added in the function declaration before the function name. A void function, also know…
  5. 5
    When you do in fact want your function to return something and pass information back to the rest of your program, C++ has you covered. Just like there are many variable types, there are many differ…
  6. 6
    When functions have a return type other than void, the function has two new requirements: - There must be a value returned from the function. - The return value must be the same type as the funct…
  7. 7
    Returning data is all well and good, but let’s say you’re visiting NYC and you’ve been told that New Yorkers usually add a 20% tip for restaurants and taxis. It would be really convenient if you co…
  8. 8
    Hang on, you may be thinking, are you limited to one parameter per function? Not at all! You can add as many as you like, but you will have to remember their order when you call the function. doubl…
  9. 9
    A function with parameters has a couple of requirements: - The function call must include the same number of arguments as there are parameters. - The corresponding arguments must be passed in…
  10. 10
    Wow! Check out all you’ve learned about C++ functions: - A function is a named group of statements that do something together. - Functions allow you to create more flexible, modular, and DRY code. …
  1. 1
    Roll up your sleeves and get yourself in the mood for functions, because it’s code challenge time! Say what? Here’s how it works: - Each exercise in this lesson has a C++ function for you to bui…
  2. 2
    introduction()
  3. 3
    average()
  4. 4
    tenth_power()
  5. 5
    first_three_multiples()
  6. 6
    needs_water()
  7. 7
    is_palindrome()
  1. 1
    Take a look at the program below. We have a void function named favorite_animal() and main() with a few statements inside. #include std::string sea_animal = “manatee”; void favorite_animal(std…
  2. 2
    Programs can grow quickly. With a few functions, you can declare the function above main() and then you can define the function below main() like this: #include // Declaration at the top: void e…
  3. 3
    If your program keeps growing, you may have to scroll through many declarations before you see main(). That doesn’t seem like the best way to do things. Plus you don’t want to keep declaring the sa…
  4. 4
    Once you set foot in the wild of C++ development, you may encounter the term “inline functions” with a couple different meanings. An inline function is a function definition, usually in a header…
  5. 5
    If you add a parameter to a function in C++, then an argument will be required when you call the function. What does “required” mean here? Well, you’ll get an error. But what if 9 times out of 10, …
  6. 6
    What if you want a function to accept an argument that can be either an int OR a double? Or what if you want some function parameters to be optional? C++ has a trick up its sleeve just for such sit…
  7. 7
    Overloading can be really tedious. Imagine you want to create a new function that works with int, float, double, and other number types. Do you really need to redefine the SAME function body over a…
  8. 8
    So how do we implement templates with actual code? Unlike regular functions, templates are entirely created in header files. Templates let us choose the type implementation right when you call the…
  9. 9
    You’ve learned quite a bit! You now know a bit about how scope works for functions and files, as well as how to make functions more flexible for different use cases: - Scope is the region of code t…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo