Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Parameters and Arguments
Master passing data to functions through parameters.
Prerequisites
Parameters vs Arguments
Before diving in, let's clarify two important terms that are often confused:
- Parameters are the variables declared in the function definition
- Arguments are the actual values passed to the function when it's called
#include <iostream>
// 'length' and 'width' are parameters
double calculateArea(double length, double width)
{
return length * width;
}
int main()
{
// '5.0' and '3.0' are arguments
double area1 = calculateArea(6.0, 4.0);
std::cout << "Area 1: " << area1 << std::endl; // Area 1: 24
double l = 5.0;
double w = 3.0;
// 'l' and 'w' are also arguments (their values are passed)
double area2 = calculateArea(l, w);
std::cout << "Area 2: " << area2 << std::endl; // Area 2: 15
return 0;
}
Function parameters
Parameters allow functions to receive input data and customize their behavior. They are declared inside the parentheses of a function:
return_type function_name(type1 parameter1, type2 parameter2, ...)
{
// function body
}
Single parameter functions
#include <iostream>
double squareNumber(double number)
{
return number * number;
}
int main()
{
std::cout << "Square of 7: " << squareNumber(7.0) << std::endl;
return 0;
}
Output:
Square of 7: 49
Multiple parameter functions
Functions can accept multiple parameters of different types:
#include <iostream>
double calculateSimpleInterest(double principal, double rate, int years)
{
return (principal * rate * years) / 100.0;
}
int main()
{
double interest = calculateSimpleInterest(1000.0, 5.0, 3);
std::cout << "Interest earned: $" << interest << std::endl;
return 0;
}
Output:
Interest earned: $150
How argument passing works
When you call a function with arguments, the values are copied into the function's parameters:
#include <iostream>
void demonstrateParameterCopying(int x, double y)
{
std::cout << "Inside function:" << std::endl;
std::cout << " x = " << x << ", y = " << y << std::endl;
// Modify the parameters
x = 100;
y = 99.9;
std::cout << "After modification inside function:" << std::endl;
std::cout << " x = " << x << ", y = " << y << std::endl;
}
int main()
{
int a = 5;
double b = 3.14;
std::cout << "Before function call:" << std::endl;
std::cout << " a = " << a << ", b = " << b << std::endl;
demonstrateParameterCopying(a, b);
std::cout << "After function call:" << std::endl;
std::cout << " a = " << a << ", b = " << b << std::endl; // Original values unchanged!
return 0;
}
Output:
Before function call:
a = 5, b = 3.14
Inside function:
x = 5, y = 3.14
After modification inside function:
x = 100, y = 99.9
After function call:
a = 5, b = 3.14
Key insight: The original variables a and b remain unchanged because the function receives copies of their values, not the variables themselves.
Parameter order and type matching
Arguments must match the order and type of parameters:
#include <iostream>
void processOrder(int quantity, double pricePerItem, bool hasDiscount)
{
double total = quantity * pricePerItem;
if (hasDiscount)
total = total * 0.9; // 10% discount
std::cout << "Quantity: " << quantity << std::endl;
std::cout << "Price per item: $" << pricePerItem << std::endl;
std::cout << "Has discount: " << (hasDiscount ? "Yes" : "No") << std::endl;
std::cout << "Total: $" << total << std::endl;
}
int main()
{
std::cout << "Customer: Alice Johnson" << std::endl;
// Correct order: string, int, double, bool
processOrder("", 3, 25.50, true);
std::cout << "\n---\n" << std::endl;
std::cout << "Customer: Bob Wilson" << std::endl;
// Another correct call
processOrder(1, 75.00, false);
return 0;
}
Output:
Customer: Alice Johnson
Quantity: 3
Price per item: $25.5
Has discount: Yes
Total: $68.85
---
Customer: Bob Wilson
Quantity: 1
Price per item: $75
Has discount: No
Total: $75
Functions with no parameters
Functions can have empty parameter lists:
#include <iostream>
int getAppVersion()
{
return 1;
}
void displayWelcome()
{
std::cout << "Welcome to our application!" << std::endl;
}
int main()
{
// Call functions with no parameters
int version = getAppVersion();
std::cout << "App version: " << version << std::endl;
displayWelcome();
return 0;
}
Output:
App version: 1
Welcome to our application!
Message: Hello, world!
Summary
Function parameters and arguments are fundamental to creating flexible, reusable functions:
Key concepts:
- Parameters are declared in the function definition
- Arguments are the values passed when calling the function
- Arguments are copied into parameters (pass by value)
- Parameter order and types must match the function definition
- Functions can have zero or many parameters
Parameters and Arguments - Quiz
Test your understanding of the lesson.
Practice Exercises
Working with Function Parameters
Practice creating functions that accept parameters to perform calculations.
Lesson Discussion
Share your thoughts and questions