C++ - Functions

Overview

Estimated time: 40–60 minutes

Encapsulate logic in functions with parameters and return values. Use default arguments and pass by reference.

Learning Objectives

  • Declare and define functions.
  • Use default parameters and pass-by-reference.

Examples

int add(int a,int b=0){ return a+b; }
void swap(int& a,int& b){ int t=a; a=b; b=t; }

Common Pitfalls

  • Default arguments must be specified in declarations, not repeated in definitions.