
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Parameter Passing Techniques in C/C++
In C we can pass parameters in two different ways. These are call by value, and call by address, In C++, we can get another technique. This is called Call by reference. Let us see the effect of these, and how they work.
First we will see call by value. In this technique, the parameters are copied to the function arguments. So if some modifications are done, that will update the copied value, not the actual value.
Example
#include <iostream> using namespace std; void my_swap(int x, int y) { int temp; temp = x; x = y; y = temp; } int main() { int a, b; a = 10; b = 40; cout << "(a,b) = (" << a << ", " << b << ")\n"; my_swap(a, b); cout << "(a,b) = (" << a << ", " << b << ")\n"; }
Output
(a,b) = (10, 40) (a,b) = (10, 40)
The call by address works by passing the address of the variables into the function. So when the function updates on the value pointed by that address, the actual value will be updated automatically.
Example
#include <iostream> using namespace std; void my_swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main() { int a, b; a = 10; b = 40; cout << "(a,b) = (" << a << ", " << b << ")\n"; my_swap(&a, &b); cout << "(a,b) = (" << a << ", " << b << ")\n"; }
Output
(a,b) = (10, 40) (a,b) = (40, 10)
Like the call by address, here we are using the call by reference. This is C++ only feature. We have to pass the reference variable of the argument, so for updating it, the actual value will be updated. Only at the function definition, we have to put & before variable name.
Example
#include <iostream> using namespace std; void my_swap(int &x, int &y) { int temp; temp = x; x = y; y = temp; } int main() { int a, b; a = 10; b = 40; cout << "(a,b) = (" << a << ", " << b << ")\n"; my_swap(a, b); cout << "(a,b) = (" << a << ", " << b << ")\n"; }
Output
(a,b) = (10, 40) (a,b) = (40, 10)
- Related Articles
- Passing Multiple ids to single parameter in MySQL?
- Passing empty parameter to a method in JavaScript
- Passing parameter with parenthesis via URL in SAP Open document
- Looping Techniques in C#
- What is the syntax for passing Scanner object as a parameter in a method using java?
- Passing by pointer Vs Passing by Reference in C++
- Passing the Assignment in C++
- What are parameter/param arrays in C#?
- What is an Optional parameter in C#?
- Passing Arrays to Function in C++
- Passing arrays to methods in C#?
- Count passing car pairs in C++
- Explain the sorting techniques in C language
- Are there benefits of passing by pointer over passing by reference in C++?
- What is parameter binding in C# ASP.NET WebAPI?
