Tutorial dan Contoh Passing Array ke Function Pemrograman C++

Tutorial dan Contoh Passing Array ke Function Pemrograman C++

Selamat datang kembali di TeknoTips.NET. TeknoTips.NET adalah media online Indonesia yang diluncurkan pada bulan Maret 2015. Kami berkomitmen menyajikan konten seputar teknologi yang berkualitas dan bermanfaat. Ulasan kami terdiri dari berbagai topik: pemrograman komputer & database, review produk, tutorial windows, linux, android, kumpulan source code & project program, referensi tugas akhir & skripsi, download ebook, game & software.

Pada kesempatan ini kita akan mengulas artikel yang berjudul : Tutorial dan Contoh Passing Array ke Function Pemrograman C++.

Arrays can be passed to a function as an argument. Consider this example to pass one-dimensional array to a function :

Passing One-dimensional Array to a Function

Example Passing One-dimensional Array to a Function :

C++ Program to display marks of 5 students by passing one-dimensional array to a function.
#include <iostream>
using namespace std;
void display(int marks[5]);
int main() {
    int marks[5] = {88, 76, 90, 61, 69};
    display(marks);
    return 0;
}
void display(int m[5]) {
    cout<<"Displaying marks: "<<endl;
    for (int i = 0; i <5; ++i) {
        cout<<"Student "<<i+1<<": "<<m[i]<<endl;
    }
}

Output
Displaying marks: 
Student 1: 88
Student 2: 76
Student 3: 90
Student 4: 61
Student 5: 69
When an array is passed as an argument to a function, only the name of an array is used as argument.
display(marks);
Also notice the difference while passing array as an argument rather than variable.
void display(int m[5]);
The argument used marks in the above code represents the memory address of first element of array marks[5]. And the formal argument int m[5] in function declaration decays to int* m;. That's why, although the function is manipulated in the user-defined function with different array name m[5], the original array is manipulated. The C++ programming language handles passing array to a function in this way to save memory and time.
Note: You need to have understanding of pointers to understand passing array to a function. Learn more: Call by reference


Passing Multidimensional Array to a Function

Multidimensional array can be passed in similar way as one-dimensional array. Consider this example to pass two dimensional array to a function:

Example Passing Multidimensional Array to a Function :

C++ Program to display the elements of two dimensional array by passing it to a function.
#include <iostream> using namespace std;
void display(int n[3][2]);
int main() {
        int num[3][2] = {
        {3, 4},
        {9, 5},
        {7, 1}

    };
    display(num);
    return 0;
}
void display(int n[3][2]) {

    cout<<"Displaying Values: "<<endl;
    for(int i = 0;  i < 3; ++ i) {
        for(int j = 0; j < 2; ++j) {
            cout<<n[i][j]<<" ";
        }
    }
}

Output
Displaying Values: 
3 4 9 5 7 1 
Multidimensional array with dimension more than 2 can be passed in similar way as two dimensional array.
Untuk mendapat notifikasi setiap artikel terbaru, masukkan e-mail anda disini
Selanjutnya cek e-mail anda untuk verifikasi.

Silahkan tuliskan komentar relevan Anda dengan jelas dan sopan, sesuai dengan topik postingan tentang ", " pada halaman dibawah ini.

0 Response to "Tutorial dan Contoh Passing Array ke Function Pemrograman C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2


Iklan Bawah Artikel