Rizq Technology – Exploring Your iDea.

All about My Experiences, Knowledge and Thoughts.

Newbie @ C++ Language eps 11

Fungsi Dalam C++

==============================================================*

#include <iostream.h>
void sayHello(char[]) ; // deklarasi fungsi sayHello()
void main()
{
char n[50];
cout<<”Masukkan nama anda : “; cin>>n;
sayHello(n);
}
void sayHello(char nama[]) // definisi funsi sayHello()
{
cout<<”Selamat datang “<<nama;
}

==============================================================*
// Penggunaan Fungsi Rekursif :
// Program mengecek sebuah bilangan integer atau bukan

#include <iostream.h>
#include <conio.h>
#include <math.h>
void cekInt(double);
void main()
{
double angka;
cout<<”Masukan sebuah angka :”;cin>>angka;
cekInt(angka);
}
void cekInt(double n)
{
if(n>1)cekInt(n-1);
else if(n<1)cekInt(-n-1);
else
{
if(n>0&&n<1)cout<<n<<”\t Bukan bilangan bulat\n”;
else cout<<n<<”\t Bilangan bulat\n”;
}
}

==============================================================*

#include <iostream.h>
#include <conio.h>
void sayHello(int);
void main()
{
sayHello(4);
}
void sayHello(int n=1)
{
for(int m=0;m<n;m++) cout<<”Halloo…\n”;
}

==============================================================*
Fungsi dengan nilai Balik
==============================================================*

#include<iostream.h>
float Kuadrat(float x);
int main()
{
int x;
cout<<”Fungsi dengan nilai balik”<<endl;
cout<<”Masukan Nilai:”;
cin>>x;
cout<<”Hasil Kuadratnya adalah :”<<Kuadrat(x);
return 0;
}
float Kuadrat(float x)
{
return(x*x);
}

==============================================================*

#include<iostream.h>
float tambah(float x, float y);
float kurang(float x, float y);
float kali(float x, float y);
float bagi(float x, float y);
int main()
{
int x,y;
cout<<”Masukan nilai pertama : “;
cin>>x;
cout<<”Masukan nilai kedua : “;
cin>>y;
cout<<”Hasil penjumlahan :”<<tambah(x,y)<<endl;
cout<<”Hasil pengurangan :”<<kurang(x,y)<<endl;
cout<<”Hasil perkalian :”<<kali(x,y)<<endl;
cout<<”Hasil pembagian :”<<bagi(x,y)<<endl;

return 0;
}
float tambah(float x,float y)
{
return(x+y);
}

float kurang(float x,float y)
{
return(x-y);
}

float kali(float x,float y)
{
return(x*y);
}

float bagi(float x,float y)
{
return(x/y);
}

==============================================================*

Categories: C / C++