Key Concepts

Review core concepts you need to learn to master this subject

C++ scope of code.

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

In C++, scope determines whether an element is available locally or globally.

Storing C++ Functions in Header Files

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

C++ function declarations are generally stored in a header file while function definitions are generally stored in another .cpp file.

Header files use the extension .hpp or .h.

An example of how a function is stored:

// my_functions.hpp void say_hi();
// my_functions.cpp void say_hi() { std::cout << ""Hi!\n""; }

It is possible to include the header in main.cpp by adding #include “”main.hpp”” at the top of the file.

function declarations in C++.

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

C++ functions typically have two parts: declaration and definition. We use header files to store function declarations, and function definitions (body of the function that defines how it is implemented) are written in .cpp files.

C++ functions definitions

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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++ inline functions

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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.

C++ default arguments

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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++ function overloading

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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.

C++ function template

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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.

function in c++.

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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

Calling C++ functions.

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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 ().

C++ function declaration and definition

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

A C++ function has two parts- declaration and 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.

void functions in C++.

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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.

C++ function return values

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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.

C++ parameters

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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

C++ function arguments

#include <iostream> using namespace std; //global variable int i=10; void print(); int main() { cout << i; // global variable is accessible in main } void print() { int j; // varialbe is local so only accessible in print i=20; cout << i; // global variable is also accessible in print }

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

  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 you choose the type implementation right when you call th…
  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