SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
PASSING AN ARRAY TO
A FUNCTION
• When passing an array to a function, we need to tell the
compiler what the type of the array is and give it a
variable name, similar to an array declaration
float a[] parameter
• We need to specify the name of the array without any
brackets
int myArray[24];
myFunction(myArray,24);
-Arrays passed call-by-reference
-Name of array is address of first element
-Function knows where the array is stored(Modifies
original memory locations)
ARRAYS ARE ALWAYS PASSED BY REFERENCE
• Arrays are automatically passed by reference. Do not use &.
• If the function modifies the array, it is also modified in the
calling environment
void Zero(int arr[], int N)
{
for (int k=0; k<N; k++)
arr[k]=0;
Function prototype:
void modifyArray( int b[], int arraySize );
-Parameter names optional in prototype
int b[] could be written int []
int arraySize could be simply int
/*Example Number 1*/
#include <iostream.h>
int Display(intint data[],int N)
{ int k;
cout<<“Array contains”<<endl;
for (k=0; k<N; k++)
cout<<data[k]<<“ “;
cout<<endl;
}
int main()
{
int a[4] = { 11, 33, 55, 77 };
Display(a, 4);
}
An int array parameter
of unknown size
Size of the array
The array argument
/*Example Number 2*/
#include <iostream.h>
int sum(int data[], int n);
void main()
{
int a[] = { 11, 33, 55, 77 };
int size = sizeof(a)/sizeof(int);
cout << "sum(a,size) = " << sum(a,size) << endl;
}
int sum(int data[], int n)
{
int sum=0;
for (int i=0; i<n;i++)
sum += data[i];
return sum;
}
The array argument
An int parameter of
unkown size
Size of the array

More Related Content

PPTX
Zener Diode Full Presentation
PPT
Data structures using c
PDF
Data file handling
PPTX
Project scheduling and tracking
PPT
Array in c
PDF
CLAUSES AND ITS TYPES.pdf
PPTX
Call by value or call by reference in C++
Zener Diode Full Presentation
Data structures using c
Data file handling
Project scheduling and tracking
Array in c
CLAUSES AND ITS TYPES.pdf
Call by value or call by reference in C++

What's hot (20)

PPTX
Strings in c++
PPTX
PPT
Stacks
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Python Functions
PPTX
Recursive Function
PPT
Function in C Language
PDF
Strings in python
PPTX
Functions in c
PDF
Arrays in Java
PPTX
Union in C programming
PPTX
Strings in C
PDF
Python tuple
PDF
Python set
PPT
PPTX
Exception handling c++
PPT
PPTX
Function C programming
PPTX
Type casting in c programming
PDF
file handling c++
Strings in c++
Stacks
Array Introduction One-dimensional array Multidimensional array
Python Functions
Recursive Function
Function in C Language
Strings in python
Functions in c
Arrays in Java
Union in C programming
Strings in C
Python tuple
Python set
Exception handling c++
Function C programming
Type casting in c programming
file handling c++
Ad

Viewers also liked (8)

PDF
Arrays and library functions
PPTX
Function in C program
PPT
DOCX
Mission and Vision
PPTX
function, storage class and array and strings
PPTX
Unit 6. Arrays
PPT
Multidimensional array in C
PDF
Function in C
Arrays and library functions
Function in C program
Mission and Vision
function, storage class and array and strings
Unit 6. Arrays
Multidimensional array in C
Function in C
Ad

Similar to Passing an Array to a Function (ICT Programming) (20)

PDF
Lecture 2.8 Arrays.pdf
PPTX
Intro to C# - part 2.pptx emerging technology
PPTX
C-Programming Arrays.pptx
PPTX
C-Programming Arrays.pptx
PPTX
Python array
PPTX
Introduction to array and string
PPTX
Array , Structure and Basic Algorithms.pptx
PPT
operator overloading concept in oops lecture12.ppt
PPT
OOP OOOOOverloading Concept lecture12.ppt
PPT
lecture12.ppt
PPT
Operator overloading in c++ is the most required.
PPTX
Programming in C sesion 2
PDF
Supporting Arrays and Allocatables in LFortran
PPT
Lec2&3 data structure
PPT
ch06.ppt
PPT
PPT
ch06.ppt
PPT
array Details
PDF
Algorithm and Programming (Array)
Lecture 2.8 Arrays.pdf
Intro to C# - part 2.pptx emerging technology
C-Programming Arrays.pptx
C-Programming Arrays.pptx
Python array
Introduction to array and string
Array , Structure and Basic Algorithms.pptx
operator overloading concept in oops lecture12.ppt
OOP OOOOOverloading Concept lecture12.ppt
lecture12.ppt
Operator overloading in c++ is the most required.
Programming in C sesion 2
Supporting Arrays and Allocatables in LFortran
Lec2&3 data structure
ch06.ppt
ch06.ppt
array Details
Algorithm and Programming (Array)

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Business Ethics Teaching Materials for college
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
RMMM.pdf make it easy to upload and study
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Pre independence Education in Inndia.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
Anesthesia in Laparoscopic Surgery in India
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Business Ethics Teaching Materials for college
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
RMMM.pdf make it easy to upload and study
Complications of Minimal Access Surgery at WLH
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Basic Mud Logging Guide for educational purpose
Pre independence Education in Inndia.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Microbial disease of the cardiovascular and lymphatic systems
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPH.pptx obstetrics and gynecology in nursing
Insiders guide to clinical Medicine.pdf
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis

Passing an Array to a Function (ICT Programming)

  • 1. PASSING AN ARRAY TO A FUNCTION
  • 2. • When passing an array to a function, we need to tell the compiler what the type of the array is and give it a variable name, similar to an array declaration float a[] parameter • We need to specify the name of the array without any brackets int myArray[24]; myFunction(myArray,24); -Arrays passed call-by-reference -Name of array is address of first element -Function knows where the array is stored(Modifies original memory locations)
  • 3. ARRAYS ARE ALWAYS PASSED BY REFERENCE • Arrays are automatically passed by reference. Do not use &. • If the function modifies the array, it is also modified in the calling environment void Zero(int arr[], int N) { for (int k=0; k<N; k++) arr[k]=0; Function prototype: void modifyArray( int b[], int arraySize ); -Parameter names optional in prototype int b[] could be written int [] int arraySize could be simply int
  • 4. /*Example Number 1*/ #include <iostream.h> int Display(intint data[],int N) { int k; cout<<“Array contains”<<endl; for (k=0; k<N; k++) cout<<data[k]<<“ “; cout<<endl; } int main() { int a[4] = { 11, 33, 55, 77 }; Display(a, 4); } An int array parameter of unknown size Size of the array The array argument
  • 5. /*Example Number 2*/ #include <iostream.h> int sum(int data[], int n); void main() { int a[] = { 11, 33, 55, 77 }; int size = sizeof(a)/sizeof(int); cout << "sum(a,size) = " << sum(a,size) << endl; } int sum(int data[], int n) { int sum=0; for (int i=0; i<n;i++) sum += data[i]; return sum; } The array argument An int parameter of unkown size Size of the array