Key Concepts

Review core concepts you need to learn to master this subject

Return Values

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

A function that returns a value must have a return statement. The data type of the return value also must match the method’s declared return type.

On the other hand, a void function (one that does not return anything) does not require a return statement.

Lesson 1 of 3
  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. …

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