1 Functions (Bag 2). 2 Topik Scope Prototypes 3 Scope Variabel yg dideklarasikan dalam badan fungsi (termasuk formal parameter) yang hanya dpt diakses.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Rully Yulian MF MCAD,MCPD,MCT,MVP VB.NET Independent IT Trainer - Application Developer
LIST ITEM. List Style … Pop List Ditampilkan dalam bentuk field dengan tombol di sebelah kanan. Ketika tombol di klik maka sekumpulan data akan muncul.
Data Types Session 2.  Primitive data types  int, float, double, char  Aggregate data types  Arrays come.
Prosedur dan Fungsi.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Stored Procedure Arfansyah, M.Kom. Mengenal Stored Procedure Stored Procedure adalah kumpulan perintah SQL yang diberi nama dan disimpan di server Stored.
Pointer. Kegunaan pointer yang utama adalah untuk menyimpan memory address dari sebuah variable (data type atau object dari class). Selain menyimpan address.
Fungsi tanpa parameter tanpa nilai balik
Chapter 7: User-Defined Functions II
Struktur Kawalan Ulangan
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Control Structures for Loops.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
TDBA66, VT-03, Lecture - Ch6_21 Function calls A function call implies –Every expression in the argument list is evaluated –If necessary, the value of.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
1 Pertemuan 09 Binary Tree Matakuliah: T0534/Struktur Data Tahun: 2005 Versi: September 2005.
Chapter 11-12, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3.
Computer Science 210 Computer Organization Introduction to C.
Functions g g Data Flow g Scope local global part 4 part 4.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Introduction to Programming Using C Modularity. 2 Contents Modularity Functions Preprocessor Comments Global variables.
Cursor. Cursors Operations Cursors adalah suatu nama area yang berisi hasil dari suatu statement SQL. 1. OPEN 2. FETCH 3. CLOSE.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
 2007 Pearson Education, Inc. All rights reserved Random Number Generation  rand function – Load – Returns "random" number between
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
C++ Course SarMag Trimester 31 Programming in C++ Prerequisite: CS1 or some programming experience Text: C++ How to Program, Deitel & Deitel.
PENDAHULUAN Dalam matematika, deret Taylor adalah representasi fungsi matematika sebagai jumlahan tak hingga dari suku-suku yang nilainya dihitung dari.
Senem Kumova Metin // CS115 // FUNCTIONS CHAPTER 5.
User-Defined Functions (cont’d) - Reference Parameters.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Dynamic memory allocation and Intraprogram Communication.
Part 2 Variabel & Data types
Variabel Dan Tipe data
Subroutine / Function Guna : Bentuk : FORTRAN
Computer Science 210 Computer Organization
C Functions -Continue…-.
Elemen Dasar C++ Pertemuan 3
Interfaces U Abd. Rohim, MT mailto:
Variabel Dan Tipe data
Dynamic memory allocation and Intraprogram Communication
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Tugas Prakarya PPT Kerajinan Bahan Lunak
Dynamic memory allocation and Intraprogram Communication
Compiled and ready to run Memory Stack /*
Pernyataan if.. Pernyataan switch..
CSI-121 Structured Programming Language Lecture 14 Functions (Part 2)
A.ERROR Kesalahan adalah perbedaan antara variabel yang diukur dan setpoint. Kesalahan dapat berupa positif atau negatif. Tujuan dari setiap skema kontrol.
Functions: Declaration, Definition, Call and return, Scope of variables, Storage classes, Recursive functions, Recursion vs Iteration.
Functions, Part 2 of 3 Topics Functions That Return a Value
Fundamental Programming
EECE.2160 ECE Application Programming
ICT HANDYBOOK SPM LA2 COMPUTER SYSTEMS 2.3 Software.
Introduction to Computing Lecture 09: Functions (Part II)
Functions, Part 2 of 3 Topics Functions That Return a Value
Standard Version of Starting Out with C++, 4th Edition
Functions, Part 2 of 3 Topics Functions That Return a Value
1. Sapto M. Suwito 2. Yetti M.M., S.Kom 3. Irni Marnida 4. Fadila Lahay 5. Endah Wahyuningsih 6. Nurlaila.
Presentation transcript:

1 Functions (Bag 2)

2 Topik Scope Prototypes

3 Scope Variabel yg dideklarasikan dalam badan fungsi (termasuk formal parameter) yang hanya dpt diakses pd saat fungsi sedang di eksekusi Buktinya, hal ini benar dari tiap blok dalam suatu program.

4 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c

5 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c Deklarasi variabel Global

6 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c Variables local pd block ini.

7 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c Scope dr variable localVar.

8 int globalVar = 0; int increment ( int paramVar ) { int localVar = 2; extern int globalVar; globalVar += localVar; return (paramVar + localVar); } int main() { int localVar = 5; for (localVar=0; localVar < 5; localVar++) { int blockVar = 1; blockVar = increment(blockVar); } printf("%d\n", globalVar); printf("%d\n", blockVar); /* Error! diluar scope */ return 0; } Contoh: scope.c Scope dr variablel blockVar.

9 Prototyping Function Fungsi harus di deklarasikan sebelum di gunakan (spt variables) Deklarasi ini disebut “prototype” Menentukan nama, parameter dan return type dari function, tanpa badan fungsi.

10 Contoh: Continuing Fraction Diberikan n dan terms, hitung: y = n... Terms jml bentuk Pecahan yg di bagi

11 Contoh: Continuing Fraction Contoh: jika n adl 2, dan terms adl 1: y = n = 

12 Fraction(n, terms) Contoh: Continuing Fraction Diberikan n dan terms, hitung: y = n...

13 Contoh: Continuing Fraction Sum(n, terms) n...

14 Fraction(n, terms - 1) Contoh: Continuing Fraction n...

15 Sum(n, 1) Contoh: Continuing Fraction 1 +1 n Term terakhir:

16 Contoh: cfraction.c Fraction ( n, terms ) { return 1 / Sum ( n, terms ) } hit pecahan berkelanjutan input n, terms set sum to 1 + Fraction(n, terms) output sum Sum ( n, terms ) { if ( last term ) then return 1 + n else return 1 + Fraction ( n, terms - 1 ) }

17 #include /*******************************\ hit pecahan berkelanjutan \*******************************/ double Fraction(double n, int terms); double Sum(double, int); int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } hit pecahan berkelanjutan input n, terms set sum to 1 + Fraction(n, terms) output sum Contoh: cfraction.c

18 #include /*******************************\ hit pecahan berkelanjutan \*******************************/ double Fraction(double n, int terms); double Sum(double, int); int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } hit pecahan berkelanjutan input n, terms set sum to 1 + Fraction(n, terms) output sum Contoh: cfraction.c Function prototypes

19 double Fraction(double n,int terms) { return (1 / Sum(n,terms)); } double Sum(double n, int terms) { if (terms==1) { return 1 + n; } else { return (1 + Fraction(n,terms-1)); } Fraction ( n, terms ) { return 1 / Sum ( n, terms ) } Sum ( n, terms ) { if ( last term ) { return 1 + n } else { return (1 + Fraction (n, terms-1)) } Contoh: cfraction.c

20 #include /*******************************\ hit pecahan berkelanjutan \*******************************/ double Fraction(double n, int terms); double Sum(double, int); int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } Header Files: Jika fungsi sering dipakai, prototipe fungsi biasanya disimpan dalam “header” file (file dg extensi.h )

21 double Fraction(double n, int terms); double Sum(double, int); funcs.h funcs.c double Fraction(double n,int terms) { return (1 / Sum(n,terms)); } double Sum(double n, int terms) { if (terms==1) { return 1 + n; } else { return (1 + Fraction(n,terms-1)); } Header Files:

22 #include #include “funcs.h” #include “funcs.c” int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } cfraction.c Header Files:

23 Berisi prototipe fungsi utk standard input/output. Standard header files biasanya disimpan dalam include directory. #include #include “funcs.h” #include “funcs.c” int main() { double n, sum; int terms; printf("Enter n and nterms: "); scanf("%lf %d", &n, &terms); sum = 1 + Fraction(n, terms); printf("%.32f\n", sum); return 0; } Header Files: