Tutoral Default Argumen Pemrograman C++

Pada kesempatan ini Mycoding.net berbagi artikel tentang yang berjudul .

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


Selain Sebagai Penyedia Panduan Belajar Database dan Tutorial Pemrograman, Kami Juga Membagikan Kumpulan Source Code Program Aplikasi dan Ebook Pemrograman Terlengkap yang Bisa Anda Dapatkan Secara Gratis di Halaman :

Kategori artikel yang terkait dengan C Plus Plus

C Plus Plus, Panduan C++, Belajar C++ Lengkap, Tutorial C++, Belajar C++ Pdf, Pemrograman C++
Untuk mendapat notifikasi artikel terbaru, masukkan e-mail anda disini :
Selanjutnya cek e-mail untuk verifikasi.
Kincai Coding Panduan Belajar Database dan Tutorial Pemrograman Terlengkap di Indonesia. Dapatkan Kumpulan Source Code Program Aplikasi dan Ebook Secara Gratis. Ikuti kami di Facebook, Instagram & Sosial Media Lainnya.