Sunday, December 11, 2016

Function-ALOKASI MEMORI

#include <iostream>
using namespace std;


void PassByValue(int b, int g){ 
            b += 3, g += 4;           
            cout << "\nWithin PassByValue(), now";          
            cout << "\n\tBoys  = " << b;    
            cout << "\n\tGirls = " << g;
}
void Reference(int &b, int &g){           
            b += 3, g += 4;           
            cout << "\nWithin Reference(), now";   
            cout << "\n\tBoys  = " << b;    
            cout << "\n\tGirls = " << g;
}
void Pointers(int *b, int *g){    
            *b += 3, *g += 4;       
            cout << "\nWithin Pointers(), now";      
            cout << "\n\tBoys  = " << *b;  
            cout << "\n\tGirls = " << *g;
}
int main(){                   
            int Boys = 3, Girls = 5;
           
            void PassByValue(int males, int females);         
            void Reference(int &m, int &f);
            void Pointers(int *u, int *v);                 

            cout << "At startup, within main()";      
            cout << "\n\tBoys  = " << Boys;           
            cout << "\n\tGirls = " << Girls; 

            cout << "\nPassing arguments by value = Copy";          
            PassByValue(Boys, Girls);       
            cout << "\nAfter calling PassByValue(), within main()"; 
            cout << "\n\tBoys  = " << Boys;           
            cout << "\n\tGirls = " << Girls;

            cout << "\nPassing arguments by reference";     
            Reference(Boys, Girls);
            cout << "\nAfter calling Reference(), within main()";      
            cout << "\n\tBoys  = " << Boys;           
            cout << "\n\tGirls = " << Girls; 

            cout << "\nPassing arguments pointers";           
            Pointers(&Boys, &Girls);         
            cout << "\nAfter calling Pointers(), within main()";         
            cout << "\n\tBoys  = " << Boys;           
            cout << "\n\tGirls = " << Girls; 
            cout << "\n";

            return 0;
}


#include <iostream>
#include <math.h>
using namespace std;

int double_it(int *a, int *b) //fungsi dobel
{
    *a *= 2; //pengalamatan variable a inputan dikalikan 2
    *b *= 2; // pengalamatan variable b inputan dikalikan 2}
int pangkat_it(int *x, int *y) //fungsi pangkat
{
    *x =(*x)*(*x); //pengalamatan variable x dengan perhitungan pangkat (*x)*(*x)
    *y =(*y)*(*y); // pengalamatan variable x dengan perhitungan pangkat (*x)*(*x)
}


int main() //program utama
{
    int x, y;
           
    cout << "Masukkan dua angka(dipisah dengan'enter') " << endl;
    cin >> x >> y;         
    double_it(&x, &y); //pemanggilan fungsi dobel
    cout << "di double kan" <<endl;
    cout << "angka pertama \t = \t" << x << endl;
    cout << "angka kedua   \t = \t" << y <<endl;
           
                pangkat_it(&x, &y); //pemanggilan fungsi pangkat
    cout << "Setelah dipangkatkan" <<endl;
    cout << "angka pertama \t = \t" << x << endl;
    cout << "angka kedua \t = \t" << y <<endl;
}

Penjelasan program
Bagian fungsi double dan fungsi pangkat (bias dilihat di source code) . *a *= 2; disini maksunya input dikalikan dua atau double  sesuai dengan definisinya “*” menghasilkan nilai yang berada disebuah alamat yang ditujukan oleh “&”(bias dilihat di program utama) nah dapat disimpulkan apapun nilai yang nantinya dihasilkan “*a” maka akan dikalikan “2”
Sementara *x =(*x)*(*x); itu hanya mengalikan sesame yg artinya seperti pemangkatan.


No comments:

Post a Comment