Let us see the Pass by value and pass by reference using structure C++.
C++ Code for Pass by value using structures
This program covers the following concepts;
- structure
- pass by value
- pass by reference
- user define function(function definition, declaration and calling)
- nested for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<iostream> using namespace std; struct counting { int i; int j; int k=0; }; void displaydata(struct counting); int main(){ counting c1; displaydata(c1); return 0; } void displaydata(struct counting c1){ for(c1.i=0;c1.i<=10;c1.i++){ for(c1.j=1;c1.j<c1.i;c1.j++){ printf("%d",c1.k); c1.k++; } printf("\n"); } } |
Output
C++ Code for Pass by reference using structures
This program covers the following concepts;
- structure
- pass by value
- pass by reference
- user define function(function definition, declaration and calling)
- nested for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<iostream> using namespace std; struct counting { int i; int j; int k=0; }; void displaydata(struct counting&); int main(){ counting c1; displaydata(c1); return 0; } void displaydata(struct counting &c1) { for(c1.i=0;c1.i<=10;c1.i++){ for(c1.j=1;c1.j<c1.i;c1.j++){ printf("%d",c1.k); c1.k++; } printf("\n"); } } |
Output