Blogger Widgets

Kamis, 22 November 2012

Materi & Latihan Soal C++





pertama-tama saya mngucapkan terimakasih udah mau ngintip sedikit blok ini..
semoga isinya dapat bermanfaat dan membantu teman-teman dalam menyelesaikan sebuah tugas ya...:)

kali ini saya mendapatkan tugas mencari materi tentang "Function" dalam bahasa inggris dan di terjemahkan dalam bahasa Indonesia.

dalam matakuliah "Computer Programming" ini saya telah mendapatkan sedikit materi dan latihan yang saya telah coba sendiri di leptop saya...

untuk lebih lanjutnya, silakan intip aja di blok ini...:)


Function

English


in  c++ program language, a program is  a collection of functions, which means that either directly in the program, as well as those stored in the file header
the function is a sub program that will allow us to process the search program.

in the program C++ there is no term prosedur.
Based mean, there are two functions , namely:
a.  User defined function
functions are defined by the user ad needed.
b. Built-in function
Functions that have been provided in the program

Types of Functions
1. Functions without a return value Syntax:
name_fungsi void ()
{statement;}

Example: Line Function
void line ()
{
cout << "-------------" << endl;
}

Function with return value (return value)
 Syntax:
_ type _ name data function ()
{statement; return
_ value _ which _ will return;}...

Example:
b) Returns a string value
/ / function declaration teststring
char * teststring ()
{
return "c + + Try Again";
}
int main ()
{
cout << teststring ();
getch ();
return 0;
}

b) Returns the value of numbers
# include <iostream>
# include <conio.h>
/ / function declaration testbilfloat testbil ()
{
return (3.14 * 2);
} int main ()
{
cout << testbil ();
getch ();
return 0;
}

function with parameter results obtained from the function can be dynamic, depending on the value of the parameter entered.
Term there are two parameters, namely:
a.  Formal Parameter: parameter defining the existing defining the function
b. Actual parameter: parameter defining the existing function calls

syntax:
tipe _ data name _ function  (parameter _1, _2 parameters, .........)
{
statement;
return
value _ which _ will _ be returned;
}

example:
b) The input parameters
# include <iostream>
# include <conio.h>

/ / function declaration plus one

int plus one (int i)
{
int result; result = i +1; return result;
}
int main ()
{
int x = 4;
cout << "Result =" << Plus one (x);
getch
();
return 0;
}

a)      The output parameters
# include <iostream>
# include <string.h>
# include <conio.h>

struct movies
{
char title [50];
}
mine;
void printmovie (movie movies)
{
cout << movie.title;
}
int main ()
{
strcpy (mine.title, "Laskar Pelangi");
cout << "My favorite movie is \ n";
printmovie (mine);
getch ();
return 0;

IIndonesi


Indonesia

 Pada bahasa pemrograman C++ suatu program adalah kumpulan dari fungsi-fungsi, baik yang didefinisikan langsung dalam program, maupun yang disimpan dalam suatu header file.

Fungsi merupakan sub program yang akan memper mudah proses penelusuran program.

Dalam bahasa C++ tidak ada istilahprocedure.
Berdasarkan pendefinisiannya, fungsi ada 2, yaitu :
a.       User defined function
Fungsi-fungsi yang didefinisikan sendiri oleh user sesuai kebutuhan
b.      Built-in function
Fungsi-fungsi yang telah disediakan didalam program


Jenis-jenis Fungsi
Fungsi tanpa nilai balik Syntax:

void nama_fungsi()
{ statement; }

Contoh : Fungsi Garis
void garis()
{
 cout<<“-------------”<<endl;
 }

Fungsi dengan nilai balik (return value)
Syntax :
tipe_data nama_fungsi()
{
statement;

return
nilai_yg_akan_dikembalikan;
}

Contoh:
a.       Mengembalikan nilai string
#include<iostream.h>
#include<conio.h>
//deklarasi fungsi teststringchar* teststring()
{
return "Coba c++ lagi";
}
int main()
{
cout<<teststring();
getch();
return 0;
}

b.      Mengembalikan nilai bilangan

#include<iostream.h>
#include<conio.h>
//deklarasi fungsi testbilfloat testbil()
{
return (3.14*2);
}
int main()
{
cout<<testbil();
getch();
return 0;
}

Fungsi dengan parameterHasil yang didapatkan dari fungsi dapatbersifat dinamis, tergantung nilaiparameter yang dimasukan.Istilah parameter ada 2, yaitu :
a.       Parameter Formal : parameter yang adapada saat pendefinisian fungsi
b.      Parameter Aktual : parameter yang adapada saat pemanggilan fungsi




Syntax :
tipe_data nama_fungsi(parameter_1,parameter_2,………)
{
statement;
return
nilai_yg_akan_dikembalikan;
}


Contoh:
1.      Dengan parameter masukan
#include<iostream.h>
#include<conio.h>
//deklarasi fungsi tambahsatu
int tambahsatu(int i)
{
int hasil;hasil=i+1;return hasil;
}
int main()
{
int x=4;cout<<"Hasil = "<<tambahsatu(x);
getch();
return 0;
}

2.      Dengan parameter keluaran
#include<iostream.h>
#include<string.h>
#include<conio.h>
struct movies
{
char title[50];
}
mine;void printmovie(movies movie)
{
cout<<movie.title;
}
int main()
{
strcpy(mine.title,"Laskar Pelangi");
cout<<"My favorite film is \n";printmovie(mine);
getch();
return 0;
}


 
}

Question
1.       Make a simple program that displays the sum by use of function.


2.       Make a programin which the denominator distribution has been determined by the operator by use of function.

Answers
1.       addition
// function example
#include <iostream>
using namespace std;
int addition (int a, int b, int c)
{
                int y;
                y=a+b+c;
                return (y);
}
int main ()
{
                int x,z;
                x = addition (5,3,4);
                z = addition (5,4,5);
                cout << "The result is " << x;
                cout <<endl;
                cout << "The result is " << z;
               
return 0;
}

==Output==

1.       division

// passing parameters by reference
#include <iostream>
using namespace std;
void duplicate (int& a, int& b, int& c)
{
a/=2;
b/=2;
c/=2;
}
int main ()
{
int x=4, y=6, z=8;
duplicate (x, y, z);
cout << "x=" ;
cout << x;
cout <<endl;
cout << "y=" ;
cout << y;
cout <<endl;
cout << "z=" ;
cout << z;
cout <<endl;

return 0;
}

==Output==


0 komentar:

:a: :b: :c: :d: :e: :f: :g: :h: :i: :j: :k: :l: :m: :n:

Posting Komentar