Tutorial dan Contoh User-defined Function Types Pemrograman C++

Tutorial dan Contoh User-defined Function Types 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 User-defined Function Types Pemrograman C++.

For better understanding of arguments and return in functions, user-defined functions can be categorised as:
  • Function with no argument and no return value
  • Function with no argument but return value
  • Function with argument but no return value
  • Function with argument and return value
Consider a situation in which you have to check prime number. This problem is solved below by making user-defined function in 4 different ways as mentioned above.
Function with no argument and no return value, Function with no argument but return value, Function with argument but no return value, Function with argument and return value, Function with no argument, Function with argument.

Function with no argument and no return value

# include <iostream>
using namespace std;
void prime();
int main() {
    prime();      // No argument is passed to prime().
    return 0;
}
// Return type of function is void because value is not returned.
void prime() {

    int num, i, flag = 0;
    printf("Enter a positive integer enter to check: ");
    cin>>num;
    for(i = 2; i <= num/2; ++i){
        if(num%i == 0) {
            flag=1; 
            break;
        }
    }
    if (flag == 1) {
        cout<<num<<" is not a prime number.";
    }
    else {
        cout<<num<<" is a prime number.";
    }
}

Function with no arguments but return value

#include <iostream>
using namespace std;
int prime();
int main() {
    int num, i, flag = 0;
    num = prime();     /* No argument is passed to prime() */
    for (i = 2; i <= num/2; ++i) {
        if (num%i == 0) {
            flag = 1;
            break;
        }
    }
    if (flag == 1) {
        cout<<num<<" is not a prime number.";
    }
    else {
        cout<<num<<" is a prime number.";
    }
    return 0;
}
// Return type of function is int
int prime() {
    int n;
    printf("Enter a positive integer to check: ");
    cin>>n;
    return n;
}

Function with argument but no return value

#include <iostream>
using namespace std;
void prime(int n);
int main() {
    int num;
    cout<<"Enter a positive integer to check: ";
    cin>>num;
    
    prime(num);  // Argument num is passed to function. 
    return 0;
}
// There is no return value to calling function. */
// Hence, return type of function is void. */  
void prime(int n) {
    int i, flag = 0;
    for (i = 2; i <= n/2; ++i) {
        if (n%i == 0) {
            flag = 1;
            break;
        }
    }
    if (flag == 1) {
        cout<<n<<" is not a prime number.";
    }
    else {
        cout<<n<<" is a prime number.";
    }
}

Function with arguments and return value.

#include <iostream>
using namespace std;
int prime(int n);
int main() {
    int num, flag = 0;
    cout<<"Enter positive enter to check: ";
    cin>>num;
    flag = prime(num); /* Argument num is passed to check() function. */
    if(flag == 1)
        cout<<num<<" is not a prime number.";
    else
        cout<<num<<" is a prime number.";
    return 0;
}
/* This function returns integer value.  */
int prime(int n){
    int i;
    for(i = 2; i <= n/2; ++i){
        if(n%i == 0)
            return 1;
    }
    return 0;
}
All four programs above gives the same output and all are technically correct program. There is no hard and fast rule on which method should be chosen. The particular method is chosen depending upon the situation and how a programmer want to solve that problem.

Tag : Function with no argument and no return value, Function with no argument but return value, Function with argument but no return value, Function with argument and return value, Function with no argument, Function with argument.
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 User-defined Function Types Pemrograman C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2


Iklan Bawah Artikel