Tutoral Default Argumen Pemrograman C++

Tutoral Default Argumen 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 : Tutoral Default Argumen Pemrograman C++.

In C++ programming, you can provide default values for function parameters. The idea behind default argument is very simple. If a function is called by passing argument/s, those arguments are used by the function. But if all argument/s are not passed while invoking a function then, the default value passed to arguments are used. Default value/s are passed to argument/s in function prototype. Working of default argument is demonstrated in the figure below:
C++ Programming Default Argument and Example Default Argument C++

Example: Default Argument


/*  C++ Program to demonstrate working of default argument    */
#include <iostream>
using namespace std;
void display(char = '*', int = 1);
int main() {
    cout<<"No argument passed:\n";
    display();
    
    cout<<"\n\nFirst argument passed:\n";
    display('#');
    
    cout<<"\n\nBoth argument passed:\n";
    display('$', 5);

    return 0;
}
void display(char c, int n){
    for(int i = 1; i <=n; ++i) {
        cout<<c;
    }
    cout<<endl;
}
Output

No argument passed:
*


First argument passed:
#


Both argument passed:
$$$$$
In the above program, you can see default value passed to arguments(in function prototype). At first, display() function is called without passing any arguments. In this case, default() function used both default arguments. Then, the function is called using only first argument. In this case, function does not use first default value passed. Function uses the actual parameter passed as first argument and takes default value(second value in function prototype) as it's second argument. When display() is invoked passing both arguments, default arguments are not used.

Note: The missing argument must be the last argument of the list, that is, if you are passing only one argument in the above function, it should be the first argument.


Tag : C++ Functions, C++ Function Types, C++ Function Overloading, C++ Default Argument, C++ Storage Class, C++ Recursion, C++ Return Reference
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 "Tutoral Default Argumen Pemrograman C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2


Iklan Bawah Artikel