Cambo Freelance
ទំព័រដើមសេវាកម្មអត្ថបទការបង្រៀនក្រុមវគ្គបណ្តុះបណ្តាលទំនាក់ទំនង
Cambo Freelance

Professional freelance team from Cambodia delivering technology-driven solutions with cultural insight and modern expertise.

ផ្ញើសាររបស់អ្នក

ត្រៀមចាប់ផ្តើមគម្រោងរបស់អ្នក? ទំនាក់ទំនងក្រុមរបស់យើង។

ទំនាក់ទំនង

សេវាកម្ម

  • Web & Mobile Development Services
  • Graphic Design & Branding Services

តំណភ្ជាប់មានប្រយោជន៍

  • ទំព័រដើម
  • សេវាកម្ម
  • ការសិក្សា
  • អំពីយើង
  • ទំនាក់ទំនង
  • តម្លៃ

© 2026 Cambo Freelance. រក្សាសិទ្ធិទាំងអស់។

TelegramFacebookLinkedInEmail
HomeTutorialsC++C++ Functions
⚡C++ Tutorials

50 lessons

Beginner(21)1C++ Introduction2C++ Getting Started3C++ Syntax4C++ Output5C++ Comments6C++ Variables7C++ Data Types8C++ Constants9C++ User Input10C++ Operators11C++ Strings12C++ Math13C++ Booleans14C++ Conditions15C++ Switch16C++ Loops17C++ Break and Continue18C++ Arrays19C++ Functions20C++ References21C++ Pointers
Intermediate(19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers
Advanced(10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44
⚡

C++ Tutorials

Lesson 19 of 50

All lessons
Beginner (21)1C++ Introduction2C++ Getting Started
26
C++ Encapsulation
27C++ Inheritance
28C++ Polymorphism
29C++ Abstraction
30C++ Files
31C++ Exception Handling
32C++ Namespaces
33C++ Structures
34C++ Enumerations
35C++ STL Introduction
36C++ Vectors
37C++ Maps
38C++ Sets
39C++ Queues
40C++ Stacks
C++ Move Semantics
45C++ Multithreading
46C++ Design Patterns
47C++ Memory Management
48C++17 Features
49C++20 Features
50C++ Performance Optimization
3
C++ Syntax
4C++ Output
5C++ Comments
6C++ Variables
7C++ Data Types
8C++ Constants
9C++ User Input
10C++ Operators
11C++ Strings
12C++ Math
13C++ Booleans
14C++ Conditions
15C++ Switch
16C++ Loops
17C++ Break and Continue
18C++ Arrays
19C++ Functions
20C++ References
21C++ Pointers
Intermediate (19)22C++ Classes23C++ Objects24C++ Constructors25C++ Access Specifiers26C++ Encapsulation27C++ Inheritance28C++ Polymorphism29C++ Abstraction30C++ Files31C++ Exception Handling32C++ Namespaces33C++ Structures34C++ Enumerations35C++ STL Introduction36C++ Vectors37C++ Maps38C++ Sets39C++ Queues40C++ Stacks
Advanced (10)41C++ Smart Pointers42C++ Templates43C++ Lambda Functions44C++ Move Semantics45C++ Multithreading46C++ Design Patterns47C++ Memory Management48C++17 Features49C++20 Features50C++ Performance Optimization
⚡C++BeginnerLesson 19 of 50CamboFreelanceJune 19, 2026

C++ Functions

Learn how to define and call C++ functions with parameters, return values, default arguments, and function overloading.


Tutorials › C++ › Functions
Beginner9 min readLesson 19 of 50

In This Lesson

  • Defining Functions
  • Parameters
  • Return Values
  • Overloading
  • Quiz

What is a Function?

A function is a reusable block of code that performs a specific task. Functions reduce repetition and make code modular.

// Declaration (prototype)
returnType functionName(parameters);

// Definition
returnType functionName(parameters) {
    // body
    return value;
}
#include <iostream>
using namespace std;

void greet() {
    cout << "Hello from a function!" << endl;
}

int main() {
    greet();   // call the function
    greet();   // call again
    return 0;
}

Output:

Hello from a function!
Hello from a function!

Parameters and Arguments

void printInfo(string name, int age) {
    cout << name << " is " << age << " years old." << endl;
}

int main() {
    printInfo("Alice", 30);
    printInfo("Bob",   25);
    return 0;
}

Default Parameters

void greet(string name = "Guest") {
    cout << "Hello, " << name << "!" << endl;
}

greet();          // Hello, Guest!
greet("Alice");   // Hello, Alice!

Return Values

int add(int a, int b) {
    return a + b;
}

double circleArea(double r) {
    return 3.14159 * r * r;
}

int main() {
    cout << add(5, 3)            << endl;   // 8
    cout << circleArea(5.0)      << endl;   // 78.54
    return 0;
}

Function Overloading

Multiple functions can share the same name if they have different parameter lists:

int    multiply(int a, int b)       { return a * b; }
double multiply(double a, double b) { return a * b; }

cout << multiply(3, 4);       // 12    (int version)
cout << multiply(2.5, 3.0);   // 7.5  (double version)

Exercise

Write a function isPrime(int n) that returns true if n is prime. Use it to print all primes from 2 to 50.

Show Solution
#include <iostream>
using namespace std;

bool isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    for (int i = 2; i <= 50; i++) {
        if (isPrime(i)) cout << i << " ";
    }
    cout << endl;
    return 0;
}

Quiz

  1. What keyword is used for a function that does not return a value?

    • A) null
    • B) none
    • C) void
    • D) empty
    Answer

    C) void

  2. What is function overloading?

    • A) Calling a function too many times
    • B) Defining multiple functions with the same name but different parameters
    • C) A function calling itself
    • D) Running a function in parallel
    Answer

    B) Overloading allows the same function name to handle different argument types/counts.

Summary

  • Functions are reusable code blocks with a name, parameters, and return type.
  • Use void for functions that return nothing.
  • Default parameters allow optional arguments.
  • Function overloading enables same name, different signatures.
← Previous: Arrays Next: C++ References →
c++functionsoverloadingbeginner
PreviousLesson 18: C++ ArraysNextLesson 20: C++ References
Back to All Tutorials