Topic 29 of 83
Parameters
Overview
Parameters are the variables defined in the function signature. Arguments are the actual values passed to the function when calling it.
Syntax
cpp
// a and b are parameters
void multiply(int a, int b) {
cout << a * b;
}
int main() {
// 5 and 10 are arguments
multiply(5, 10);
}Common Pitfalls
- Passing the wrong data type as an argument, causing implicit and potentially destructive type casting.
Interview Tips
- Know that parameters act as local variables inside the function scope.
Real-World Example
Passing variables as arguments.
example
cpp
#include <iostream>
#include <string>
using namespace std;
void greetUser(string name, int age) {
cout << "Hi " << name << ", you are " << age << " years old.\n";
}
int main() {
string myName = "Alice";
int myAge = 25;
greetUser(myName, myAge);
return 0;
}