wleci
AboutProjectsBlogContact
Contact
Back to blog
FunctionsBasicsProgramming

Stage 4: Functions and Argument Passing in C++

An in-depth analysis of function definitions, prototypes, and the differences between passing by value and by reference.

February 11, 20263 min read
Share:

Functions in C++: Modularity and Reusability

Functions are the basic building blocks of a program, allowing you to break down complex problems into smaller, manageable pieces. At your level, it is crucial not only to know how to write a function but also how to efficiently transfer data to it.


1. Function Structure

Every function consists of a return type, a name, a parameter list, and the function body.

cpp
return_type function_name(parameters) { // body return value; // if the type is not void }

Important: If the function is located below the main() function, you must place its prototype (declaration) at the top of the file.

cpp
double calculateAverage(int a, int b); // Prototype

2. Passing Arguments by Value

This is the default way in C++. The function receives a copy of the variable. Changes made inside the function do not affect the original variable.

cpp
void increase(int x) { x = x + 10; // Only changes the local copy } int main() { int number = 5; increase(number); // number is still 5 }

3. Passing by Reference (&)

By using the & symbol, you create an alias for the original variable. The function operates directly on the data from the calling site. This is more efficient (no copying of large structures) and allows a function to "return" more than one value.

cpp
void actuallyIncrease(int &x) { x = x + 10; // Changes the original }

4. Passing Arrays to Functions

Arrays are specific – in C++, an array passed to a function automatically "decays" into a pointer to its first element. This means the function always operates on the original, and you must additionally pass the size of the array.

cpp
void printArray(int tab[], int size) { for(int i = 0; i < size; i++) { std::cout << tab[i] << " "; } }

Pitfalls and Best Practices for Exams

  1. Returning Values (return): Remember that a function terminates immediately upon hitting a return. If a function is supposed to return an int, ensure every logical path (e.g., inside an if) ends with a return statement.
  2. Local Variables: Variables declared inside a function cease to exist when it finishes. Never try to return a reference to a local variable!
  3. void: Use void when a function is meant to perform an action (e.g., print text) rather than compute a value. In void functions, the return; statement (without a value) is used to exit the function early.

Example: Swap Function

This is a classic exam task testing the understanding of references:

cpp
void swap(int &a, int &b) { int temp = a; a = b; b = temp; }

You might also like

PointersMemory

Stage 11: Pointer Arithmetic in C++

Understanding how C++ operates on memory addresses. Learn why ptr++ is more than just adding one.

3 min read
PointersMemory Management

Stage 10: Pointers, Address-of, and Dereference Operators

Understanding the foundations of memory addressing in C++: how pointers and low-level operators work.

3 min read
StringsC-Style

Stage 6: C-Style String Handling (char[])

A guide to low-level string processing as null-terminated character arrays, following exam constraints.

3 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland