Passing Arguments by Value/Pointer/Reference
Pass by value
Variables created inside a function are accessible only within that function. This rule also applies when passing arguments. Parameters in the function signature have local scope and are passed by value, meaning their values are copied into the function and stored in separate variables (just like borrowing a library book instead of taking the original).
main.cpp
1234567891011121314151617181920#include <iostream> // Function that takes an argument by value void modify(int num) { // Modify the parameter (local copy of the argument) num = num * 2; } int main() { int number = 5; std::cout << "Before function call: " << number << std::endl; // Call the function and pass 'number' by value modify(number); // Varibale in the main function remains unchanged std::cout << "After function call: " << number << std::endl; }
Pass by pointer
Passing arguments by value isn’t always efficient. Copying values uses extra memory, and sometimes a function needs to modify the original variable directly. In such cases, it’s better to pass the variable by pointer, which means sending its memory address instead of its value. This is done using the pointer operator (*) in the function parameters.
main.cpp
1234567891011121314151617181920212223#include <iostream> // Function that takes an argument using pointer void modify(int* p_num) { // Modify the value at the memory address pointed by `p_num` // '*' is used to extract the value from the address (pointer) *p_num = (*p_num) * 2; std::cout << "Inside function: " << *p_num << std::endl; } int main() { int number = 5; std::cout << "Before function call: " << number << std::endl; // Call the function and pass the address of 'number' using a pointer // '&' is used to get an address of the variable modify(&number); // 'number' in the main function is modified through the pointer std::cout << "After function call: " << number << std::endl; }
Pass by reference
Passing by reference means giving a function direct access to a variable’s memory, allowing it to modify the original value without using the dereference operator (*).
Passing by reference uses the & operator in the function parameters. It works similarly to passing by pointer, but with two key differences:
- You don’t need to use
*to access the variable inside the function. - You don’t need to use
&when calling the function, only in its declaration.
main.cpp
1234567891011121314151617181920#include <iostream> // Function that takes an argument by reference void modify(int& num) { // Modify the parameter directly (no need for dereferencing) num = num * 2; } int main() { int number = 5; std::cout << "Before function call: " << number << std::endl; // Call the function and pass `number` by reference modify(number); // `number` in the main function is modified directly through the reference std::cout << "After function call: " << number << std::endl; }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 5
Passing Arguments by Value/Pointer/Reference
Veeg om het menu te tonen
Pass by value
Variables created inside a function are accessible only within that function. This rule also applies when passing arguments. Parameters in the function signature have local scope and are passed by value, meaning their values are copied into the function and stored in separate variables (just like borrowing a library book instead of taking the original).
main.cpp
1234567891011121314151617181920#include <iostream> // Function that takes an argument by value void modify(int num) { // Modify the parameter (local copy of the argument) num = num * 2; } int main() { int number = 5; std::cout << "Before function call: " << number << std::endl; // Call the function and pass 'number' by value modify(number); // Varibale in the main function remains unchanged std::cout << "After function call: " << number << std::endl; }
Pass by pointer
Passing arguments by value isn’t always efficient. Copying values uses extra memory, and sometimes a function needs to modify the original variable directly. In such cases, it’s better to pass the variable by pointer, which means sending its memory address instead of its value. This is done using the pointer operator (*) in the function parameters.
main.cpp
1234567891011121314151617181920212223#include <iostream> // Function that takes an argument using pointer void modify(int* p_num) { // Modify the value at the memory address pointed by `p_num` // '*' is used to extract the value from the address (pointer) *p_num = (*p_num) * 2; std::cout << "Inside function: " << *p_num << std::endl; } int main() { int number = 5; std::cout << "Before function call: " << number << std::endl; // Call the function and pass the address of 'number' using a pointer // '&' is used to get an address of the variable modify(&number); // 'number' in the main function is modified through the pointer std::cout << "After function call: " << number << std::endl; }
Pass by reference
Passing by reference means giving a function direct access to a variable’s memory, allowing it to modify the original value without using the dereference operator (*).
Passing by reference uses the & operator in the function parameters. It works similarly to passing by pointer, but with two key differences:
- You don’t need to use
*to access the variable inside the function. - You don’t need to use
&when calling the function, only in its declaration.
main.cpp
1234567891011121314151617181920#include <iostream> // Function that takes an argument by reference void modify(int& num) { // Modify the parameter directly (no need for dereferencing) num = num * 2; } int main() { int number = 5; std::cout << "Before function call: " << number << std::endl; // Call the function and pass `number` by reference modify(number); // `number` in the main function is modified directly through the reference std::cout << "After function call: " << number << std::endl; }
Bedankt voor je feedback!