Back to C++
2026-01-165 min read

Example 2: Function with Parameters (C++)

Learn Example 2: Function with Parameters (C++) step by step with clear examples and exercises.

Why This Matters

In this lesson, we delve deeper into C++ functions, focusing on those that take parameters. Understanding how to create and use functions with parameters is crucial for writing modular, efficient, and reusable code in C++. Functions with parameters allow us to pass data into the function and have it perform actions based on that input, making them a fundamental aspect of programming.

Prerequisites

To fully grasp the concept of functions with parameters in C++, you should have a solid understanding of:

  1. Basic C++ syntax (variables, operators, control structures)
  2. Understanding of data types (int, float, char, etc.)
  3. Basic I/O operations (cin, cout)
  4. Concept of functions (without parameters)
  5. Understanding of pointers and references (optional but recommended for a deeper understanding of passing arguments by reference)

Core Concept

A function is a self-contained block of code that performs a specific task. Functions with parameters allow us to pass data into the function and have it perform actions based on that input. In C++, we define a function with parameters using the following syntax:

return_type function_name(parameter1 type parameter1_name, parameter2 type parameter2_name, ...) {
// Function body
}

Here's a breakdown of the parts:

  • return_type is the data type of the value that the function will return (optional). If no return value is required, use void.
  • function_name is the name given to the function.
  • parameter1, parameter2, etc., are the inputs the function accepts. Each parameter has a specific data type and a unique name.

Function Call

To call (invoke) a function with parameters, we use its name followed by parentheses containing the arguments that correspond to the function's parameters:

function_name(argument1, argument2, ...);

The arguments passed to the function must match the data type and order of the function's parameters.

Passing Arguments by Value and Reference

In C++, there are two ways to pass arguments to functions:

  1. Pass by value: The entire variable is copied into the function. This can be inefficient for large objects.
  2. Pass by reference: A reference to the original variable is passed to the function. Changes made inside the function will affect the original variable. To pass an argument by reference, use the & symbol before the parameter name:
void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

In this example, we define a function called swap that takes two integer references as parameters and swaps their values. Passing by reference improves performance for large objects and allows the function to modify the original variables.

Worked Example

Let's create a simple function that calculates the area of a rectangle using its length and width as parameters:

#include <iostream>
using namespace std;

double calculateArea(double length, double width) {
return length * width;
}

int main() {
double length = 5.0;
double width = 3.0;

cout << "The area of the rectangle is: " << calculateArea(length, width);

return 0;
}

In this example, we define a function called calculateArea that takes two double parameters (length and width) and returns their product as the area of the rectangle. In the main function, we create variables for the length and width, call the calculateArea function with those values, and output the result.

Common Mistakes

  1. Forgetting to include necessary headers: Make sure you include all required header files (e.g., ``) at the beginning of your code.
  2. Incorrect parameter data types: Ensure that the data types of function parameters match those of the arguments passed when calling the function.
  3. Not returning a value from a function with a return type: If you define a function with a return type, make sure to include a return statement in the function body.
  4. Passing arguments by value for large objects: Pass arguments by reference for large objects to avoid unnecessary memory allocation and improve performance.
  5. Not using parentheses when calling functions: Always use parentheses when calling functions, even if they have only one parameter.
  6. ### Subheadings under Common Mistakes:
  • Function not defined before usage: Make sure the function is defined before it's called in the code.
  • Returning a value from a void function: If the function is declared as void, it should not return any value, and including a return statement will cause a compile error.

Practice Questions

  1. Write a function called addNumbers that takes two integers as parameters and returns their sum.
  2. Write a function called printGreeting that takes a string as a parameter and prints it as a greeting.
  3. Modify the swap function from the Core Concept section to work with floating-point numbers instead of integers.
  4. ### Subheadings under Practice Questions:
  • Function with multiple parameters: Write a function that takes three integer parameters and returns their sum.
  • Function with default values: Write a function that calculates the area of a circle, taking the radius as a parameter with a default value of 1 if no argument is provided.

FAQ

  1. Why should I use functions with parameters?

Using functions with parameters makes your code more modular, easier to read, and reusable. It allows you to break down complex problems into smaller, manageable tasks.

  1. What is the difference between pass by value and pass by reference in C++?

Pass by value copies the entire variable into the function, while pass by reference creates a reference to the original variable. Changes made inside the function will affect the original variable when passing by reference.

  1. How do I know if a function requires arguments or not?

Check the function definition for parameters. If there are no parameters listed within parentheses, the function does not require any arguments.

  1. What happens if I pass an incorrect data type as an argument to a function in C++?

If you pass an incorrect data type as an argument to a function in C++, the program will compile without errors but may produce unexpected results or cause runtime errors.

  1. Can I change the value of an argument passed by value inside a function in C++?

No, since the entire variable is copied into the function when passing by value, changes made inside the function will not affect the original variable's value. However, if you pass the variable by reference, changes made inside the function will affect the original variable's value.

  1. Can I return multiple values from a function in C++?

No, functions in C++ can only return one value. To return multiple values, consider using structures or classes with multiple member variables.

  1. What is the purpose of the const keyword when declaring parameters in C++?

The const keyword indicates that a parameter should not be modified within the function. This can help prevent accidental modifications and improve code readability.

Example 2: Function with Parameters (C++) | C++ | XQA Learn