C++ Tutorial: Declaring and Writing Functions

Objective

Learn how to declare and define functions in C++, pass variables by value and reference, and understand their use cases.


1. Introduction to Functions

  • Definition: A function is a reusable block of code that performs a specific task.
  • Structure:
    return_type function_name(parameters) { // Function body return value; // Optional, depends on return_type }

Key Points

  • Functions improve code modularity and readability.
  • They avoid redundancy by enabling reuse.

2. Declaring and Defining Functions

Function Declaration (Prototype)

  • Specifies the function’s name, return type, and parameters (without implementation).
  • Placed at the top of the file or in a header file.
    int add(int a, int b); // Declaration

Function Definition

  • Provides the actual implementation.
  • Placed after the main() function or in another file.
    int add(int a, int b) { return a + b; // Implementation }

Code Example

#include 
using namespace std;

// Function declaration
int add(int a, int b);

int main() {
int result = add(5, 3);
cout << "The sum is: " << result << "\n";
return 0;
}

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

3. Passing Variables to Functions

1. Pass by Value

  • A copy of the variable is passed to the function.
  • Changes inside the function do not affect the original variable.
Code Example
#include 

void increment(int num) {
num += 1; // Modifies only the local copy
cout << "Inside function: " << num << "\n";
}

int main() {
int x = 5;
increment(x);
cout << "Outside function: " << x << "\n"; // Original value remains unchanged
return 0;
}

Output

Inside function: 6
Outside function: 5

2. Pass by Reference

  • A reference to the original variable is passed.
  • Changes inside the function affect the original variable.
Code Example
#include 

void increment(int &num) { // Reference parameter
num += 1; // Modifies the original variable
cout << "Inside function: " << num << "\n";
}

int main() {
int x = 5;
increment(x);
cout << "Outside function: " << x << "\n"; // Original value is updated
return 0;
}

Output

Inside function: 6
Outside function: 6

3. Pass by Pointer

  • A pointer to the variable is passed.
  • Changes inside the function affect the original variable.
Code Example
#include 

void increment(int *num) { // Pointer parameter
*num += 1; // Dereference to modify the original variable
cout << "Inside function: " << *num << "\n";
}

int main() {
int x = 5;
increment(&x); // Pass the address of the variable
cout << "Outside function: " << x << "\n"; // Original value is updated
return 0;
}

Output

Inside function: 6
Outside function: 6

4. When to Use Each Passing Method

MethodDescriptionUse Case
Pass by ValueCreates a copy of the variable; changes do not affect the original.Use when you don’t want the original variable modified.
Pass by ReferencePasses the original variable; changes directly affect it.Use when you want the function to modify the variable or avoid copying large data.
Pass by PointerSimilar to pass by reference but requires explicit use of pointers and addresses.Use when working with dynamic memory or null values.

5. Function with Multiple Parameters

Code Example

#include 

// Function declaration
void swap(int &a, int &b);

int main() {
int x = 10, y = 20;

cout << "Before swapping: x = " << x << ", y = " << y << "\n";
swap(x, y);
cout << "After swapping: x = " << x << ", y = " << y << "\n";

return 0;
}

// Function definition
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

Output

Before swapping: x = 10, y = 20
After swapping: x = 20, y = 10

6. Function Overloading

  • Functions with the same name but different parameter types or counts.

Code Example

#include 

// Overloaded functions
int multiply(int a, int b) {
return a * b;
}

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

int main() {
cout << "Int multiply: " << multiply(3, 4) << "\n";
cout << "Double multiply: " << multiply(3.5, 4.5) << "\n";
return 0;
}

Output

Int multiply: 12
Double multiply: 15.75

7. Exercises

  1. Write a function that takes two integers as input and returns their greatest common divisor (GCD).
  2. Write a program to swap two floating-point numbers using pass by reference.
  3. Implement a function to calculate the factorial of a number using pass by value.

Conclusion

  • Functions are essential for modular, readable, and reusable code.
  • Choose the appropriate passing method (value, reference, or pointer) based on the use case.
Scroll to Top