Tutoral Default Argumen Pemrograman C++

Tutoral Default Argumen Pemrograman C++

Pada kesempatan ini TeknoTips.NET akan mengulas tentang "" dengan judul artikel "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

Keyword : C Plus Plus English, Cpp Functions,
Untuk mendapat notifikasi setiap artikel terbaru, masukkan e-mail anda disini : Tuliskan komentar relevan anda dibawah ini dengan jelas & sopan, sesuai dengan topik ulasan mengenai C Plus Plus English Terbaru 2019.

Untuk menghindari spam dan tautan aktif, komentar akan dimoderasi. Dapatkan balasan dari melalui e-mail anda :

Komentar artikel "Tutoral Default Argumen Pemrograman C++"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel