Function
Gambaran fungsi OUTPUT INPUT FUNCTION Argumen/Parameter Return value Parameter dan return value sifatnya opsional, bisa ada, bisa pula tidak ada.
What is a function? The function is one of the most basic things to understand in C programming. A function is a sub-unit of a program which performs a specific task. We have already (without knowing it) seen one function from the C library – printf. We need to learn to write our own functions. Functions take arguments (variables) and may return an argument. Think of a function as extending the C language to a new task. Or perhaps variables are NOUNS functions are VERBS.
An example function Prototype(declaration) the function Call the function, i dan j sebagai input function header Function definition Argumen / Parameter Return value type : menandakan tipe data yang di-return oleh fungsi tsb
Mengenai Prototype fungsi Prototype fungsi harus dituliskan dalam source code jika penulisan fungsi ada di bawah fungsi lain yang memanggil fungsi tsb Tidak wajib dituliskan jika penulisan fungsi itu di atas fungsi lain yg memanggil fungsi tsb
What are these prototype things? A prototype tells your C program what to expect from a function - what arguments it takes (if any) and what it returns (if any) Prototypes should go before main() #include finds the prototypes for library functions (e.g. printf) A function MUST return the variable type we say that it does in the prototype.
Variabel lokal dan global // function example #include <iostream> using namespace std; int z; int subtraction (int a, int b) { int r; r=a-b; return (r); } int main () { int x=5, y=3; z = subtraction (7,2); cout << "The result is " << z << '\n'; return 0; Variabel global Variabel lokal
Variabel lokal dan global Variabel global akan tetap dialokasikan di memori selama program masih berjalan Variabel lokal hanya dialokasikan di memori ketika fungsi dijalankan Ketika suatu fungsi selesai dieksekusi, variable lokal di dalamnya akan dihancurkan (destroyed) dihapus dari memori Jadi penggunaan var lokal lebih hemat memori
Argumen/Parameter #include <iostream> Merupakan input suatu fungsi Nilai suatu variabel yang dijadikan argumen tidak akan berubah walaupun fungsi berusaha merubahnya. Contoh: #include <iostream> using namespace std; int addition (int a, int b) { int r; r=a+b; a=a+2; //kita coba ubah argumen 1(a) dengan menambahnya dengan 2 return (r); } int main () { int z,in1=5,in2=3; z = addition (in1,in2); //kita cek input 1 sebelum eksekusi fungsi cout << “input 1 sebelum eksekusi fungsi : “<<in1; //kita cek input 1(in1) apakah berubah atau tidak cout << “input 1 setelah eksekusi fungsi : “<<in1; cout << "The result is " << z; return 0;
Argumen output Argumen sebenarnya juga bisa dijadikan output suatu fungsi (akan dijelaskan pada materi yg akan datang)
Fungsi dengan argumen berupa array / pointer Contoh 1: argumen berupa array, input berupa array //fungsi untuk menghitung rerata 10 data int rerata(int data1[10]) { int i,sum=0; for(i=0;i<10;i++) sum=sum+data1[i]; return sum/10; } //rutin main untuk memangil main( ) { int data2[10]={1,3,4,5,6,5,7,6,2,8};//data yang akan dijadikan input fungsi ‘rerata’ int rata2; rata2=rerata(data2); …. Tipe dan ukuran harus sama
Fungsi dengan argumen berupa array / pointer Contoh 2: argumen berupa pointer, input berupa array //fungsi untuk menghitung rerata 10 data, argumen bertipe pointer to int int rerata(int *data1) { int i,sum=0; for(i=0;i<10;i++) { sum=sum+*data1; data1++ } return sum/10; //rutin main untuk memangil main( ) { int data2[10]={1,3,4,5,6,5,7,6,2,8};//data yang akan dijadikan input fungsi ‘rerata’ int rata2; rata2=rerata(data2); //fungsi dengan argumen berupa pointer bisa diberi input berupa array . . .
void functions A function doesn't have to take or return arguments. We prototype such a function using void. Prototype (at top of file remember) Function takes and returns void (no arguments) Another prototype void odd_or_even (int); Function which takes one int arguments and returns none
Functions can access other functions Once you have written a function, it can be accessed from other functions. We can therefore build more complex functions from simpler functions
Notes about functions A function can take any number of arguments mixed in any way. A function can return at most one argument. When we return from a function, the values of the argument HAVE NOT CHANGED. We can declare variables within a function just like we can within main() - these variables will be deleted when we return from the function
Where do functions go in the program Generally speaking it doesn't matter too much. main() is a function just like any other (you could even call it from other functions if you wanted. It is common to make main() the first function in your code. Functions must be entirely separate from each other. Prototypes must come before functions are used. A usual order is: Prototypes THEN main THEN other functions.