This Cpp Tutorial contains
Write a program which declares an array, initialize it then pass to a function. Function takes square of array values and update it. Show updated values in main function.
Concept used:
Note: Function return type is void why?
As we know array name is constant pointer memory location if we get this location we can update it after updating if we access this memory location any where in the program we will get updated values. So no necessarily needs to return array or pointer.
First way
As we know array name is a constant pointer if we can get the this pointer we can access all values of array.
declaring array in function parameter
Prototype: void UpdateArray(int array[] );
c++ code
Second way
Prototype: void UpdateArray(int *array );
Declaring an integer pointer in function parameter
This pointer will receive arrays name(constant pointer) and we can access all values and update too.
Third way
In this way we pass two arguments one is arrays name and second is its size
In function receiving parameters we declares an array or integer pointer (your choice) to receive address of array and another integer variable size which contains the size of array.
Prototype: void UpdateArray(int *array, int size );
more Arrays Examples Here
C++ Simple Examples
For best practice do experiment with codes and examine output
if any confusion found dry run the code on a paper.
- 3 ways to pass an array to a function
- Each way and its usage
- 3 c++ codes examples
- How codes are working
- Sample output
- Compiler used "C++ CodeBlocks Compiler"
Write a program which declares an array, initialize it then pass to a function. Function takes square of array values and update it. Show updated values in main function.
Concept used:
- Passing array to a function
- for loop
- Declare array of size 10
- Initialize it using for loop
- Calling "UpdateArray" function and passing array as argument
- Take squares of all values and update it
- Display final result in main function
Note: Function return type is void why?
As we know array name is constant pointer memory location if we get this location we can update it after updating if we access this memory location any where in the program we will get updated values. So no necessarily needs to return array or pointer.
First way
As we know array name is a constant pointer if we can get the this pointer we can access all values of array.
declaring array in function parameter
Prototype: void UpdateArray(int array[] );
c++ code
- #include <iostream>
- using namespace std;
- void UpdateArray(int array[])
- {
- for(int i=0;i<=9;i++)
- array[i]=array[i]*array[i];
- }
- int main()
- {
- int array[10];
- for(int i=0;i<=9;i++)
- array[i]=i+1;
- UpdateArray(array);
- // Updated Values of Array
- for(int i=0;i<=9;i++)
- cout<<array[i]<<endl;
- return 0;
- }
Second way
Prototype: void UpdateArray(int *array );
Declaring an integer pointer in function parameter
This pointer will receive arrays name(constant pointer) and we can access all values and update too.
- #include <iostream>
- using namespace std;
- void UpdateArray(int *array)
- {
- for(int i=0;i<=9;i++)
- array[i]=array[i]*array[i];
- }
- int main()
- {
- int array[10];
- for(int i=0;i<=9;i++)
- array[i]=i+1;
- UpdateArray(array);
- // Updated Values of Array
- for(int i=0;i<=9;i++)
- cout<<array[i]<<endl;
- return 0;
- }
Third way
In this way we pass two arguments one is arrays name and second is its size
In function receiving parameters we declares an array or integer pointer (your choice) to receive address of array and another integer variable size which contains the size of array.
Prototype: void UpdateArray(int *array, int size );
- #include <iostream>
- using namespace std;
- void UpdateArray(int *array, int size)
- {
- for(int i=0;i<size;i++)
- array[i]=array[i]*array[i];
- }
- int main()
- {
- int array[10];
- for(int i=0;i<=9;i++)
- array[i]=i+1;
- UpdateArray(array,10);
- // Updated Values of Array
- for(int i=0;i<=9;i++)
- cout<<array[i]<<endl;
- return 0;
- }
more Arrays Examples Here
C++ Simple Examples
For best practice do experiment with codes and examine output
if any confusion found dry run the code on a paper.
0 comments:
Post a Comment