1 FP 201 PROGRAMMING FUNDAMENTALS FP 201 Programming

  • Slides: 28
Download presentation
1 FP 201 PROGRAMMING FUNDAMENTALS FP 201 Programming Fundamentals 1. 0 • Introduction To

1 FP 201 PROGRAMMING FUNDAMENTALS FP 201 Programming Fundamentals 1. 0 • Introduction To C++ Programming 2. 0 • Basic C++ Program Structure 3. 0 • Program Control 4. 0 • Array And Structures 5. 0 • Function 6. 0 • Pointer 7. 0 • Secure Programming in C++ © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

2 5. 0 FUNCTION 5. 1 Apply The Functions in Programming FP 201 PROGRAMMING

2 5. 0 FUNCTION 5. 1 Apply The Functions in Programming FP 201 PROGRAMMING FUNDAMENTALS

3 FP 201 PROGRAMMING FUNDAMENTALS LEARNING OUTCOMES TOPIC 5. 1 By the end of

3 FP 201 PROGRAMMING FUNDAMENTALS LEARNING OUTCOMES TOPIC 5. 1 By the end of this chapter students shall be able to : 1 • Define a function 2 • Explain the following types of functions: Builtin function, User-defined function 3 • Describe the components of functions 4 • Identify the function calls: call by value, call by reference.

4 FP 201 PROGRAMMING FUNDAMENTALS DEFINE FUNCTION • A function is a group of

4 FP 201 PROGRAMMING FUNDAMENTALS DEFINE FUNCTION • A function is a group of statements that is executed when it is called from some point of the program. • There are two types of function : ▫ Built-in function ▫ User defined function © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

5 FP 201 PROGRAMMING FUNDAMENTALS Built -in Function • These function are stored in

5 FP 201 PROGRAMMING FUNDAMENTALS Built -in Function • These function are stored in what are known as standard header files (with extension. h) • Before you can use any of the function contained in a header file, you must include the header file in your program. • Example: The function strlen ( ) calculates the length of string. It takes the form strlen (string) cout<<strlen (“good day”); The above statement will display the value of 8. The embedded blank in the string is also counted as a character. This function is in header file string. h, you must add this header file to use this function. © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

6 FP 201 PROGRAMMING FUNDAMENTALS User Defined Function • User-defined functions are created by

6 FP 201 PROGRAMMING FUNDAMENTALS User Defined Function • User-defined functions are created by you, the programmer © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

7 FP 201 PROGRAMMING FUNDAMENTALS Component Of Function • Function Name Function Body Local

7 FP 201 PROGRAMMING FUNDAMENTALS Component Of Function • Function Name Function Body Local Variable Return Statement Parameters declaration list Function prototypes © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

8 FP 201 PROGRAMMING FUNDAMENTALS Function Name • strlen() double kira. Luas(int r) {

8 FP 201 PROGRAMMING FUNDAMENTALS Function Name • strlen() double kira. Luas(int r) { double luas. Bulatan; luas. Bulatan=pi*r*r; return (luas. Bulatan); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

9 FP 201 PROGRAMMING FUNDAMENTALS Function Body double kira. Luas(int r) { double luas.

9 FP 201 PROGRAMMING FUNDAMENTALS Function Body double kira. Luas(int r) { double luas. Bulatan; luas. Bulatan=pi*r*r; return (luas. Bulatan); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

10 FP 201 PROGRAMMING FUNDAMENTALS Local Variable • Variable that is declared inside a

10 FP 201 PROGRAMMING FUNDAMENTALS Local Variable • Variable that is declared inside a block. It is accessible only from within that block. double kira. Luas(int r) { double luas. Bulatan; luas. Bulatan=pi*r*r; return (luas. Bulatan); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

11 FP 201 PROGRAMMING FUNDAMENTALS Return Statement double kira. Luas(int r) { double luas.

11 FP 201 PROGRAMMING FUNDAMENTALS Return Statement double kira. Luas(int r) { double luas. Bulatan; luas. Bulatan=pi*r*r; return (luas. Bulatan); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

12 FP 201 PROGRAMMING FUNDAMENTALS Function Prototype/Declaration double kira. Luas(int); -Declare function before main().

12 FP 201 PROGRAMMING FUNDAMENTALS Function Prototype/Declaration double kira. Luas(int); -Declare function before main(). © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

13 FP 201 PROGRAMMING FUNDAMENTALS Parameter Declaration List double kira. Luas(int); © 2011/2012 |

13 FP 201 PROGRAMMING FUNDAMENTALS Parameter Declaration List double kira. Luas(int); © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

14 FP 201 PROGRAMMING FUNDAMENTALS Function Prototype/declaration double kira. Luas(int) ; double kira. Luas(int

14 FP 201 PROGRAMMING FUNDAMENTALS Function Prototype/declaration double kira. Luas(int) ; double kira. Luas(int r) { double luas. Bulatan; luas. Bulatan=pi*r*r; return (luas. Bulatan); } Local variable © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR Parameters declaration list Function name Function body/define function Return statement

15 FP 201 PROGRAMMING FUNDAMENTALS #include <iostream. h> #define PI 3. 14 double kira.

15 FP 201 PROGRAMMING FUNDAMENTALS #include <iostream. h> #define PI 3. 14 double kira. Luas(int) ; void main() { cout<<kira. Luas(2); } double kira. Luas(int r) { double luas. Bulatan; luas. Bulatan=PI*r*r; return (luas. Bulatan); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

16 FP 201 PROGRAMMING FUNDAMENTALS #include <iostream. h> #define PI 3. 14 double kira.

16 FP 201 PROGRAMMING FUNDAMENTALS #include <iostream. h> #define PI 3. 14 double kira. Luas(int) ; void main() { cout<<kira. Luas(2); } double kira. Luas(int r) { double luas. Bulatan; luas. Bulatan=PI*r*r; return (luas. Bulatan); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR Output :

17 #include <iostream. h> #define PI 3. 14 double luas. Bulatan; double luas. Segiempat;

17 #include <iostream. h> #define PI 3. 14 double luas. Bulatan; double luas. Segiempat; double jum; void kira. Luas 1(int) ; void kira. Luas 2(int, int) ; void Total(); void main() { double radius; cout << "enter radius: "; cin >> radius; kira. Luas 1(radius); kira. Luas 2(4, 5); Total(); } void kira. Luas 1(int r) { luas. Bulatan = PI * r; } void kira. Luas 2(int a, int b) { luas. Segiempat = a * b; } void Total() { jum = luas. Bulatan + luas. Segiempat; cout << jum; } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR FP 201 PROGRAMMING FUNDAMENTALS

18 #include <iostream. h> #define PI 3. 14 double luas. Bulatan; double luas. Segiempat;

18 #include <iostream. h> #define PI 3. 14 double luas. Bulatan; double luas. Segiempat; double jum; void kira. Luas 1(int) ; void kira. Luas 2(int, int) ; void Total(); void main() { double radius; cout << "enter radius: "; cin >> radius; kira. Luas 1(radius); kira. Luas 2(4, 5); Total(); } void kira. Luas 1(int r) { luas. Bulatan = PI * r; } void kira. Luas 2(int a, int b) { luas. Segiempat = a * b; } void Total() { jum = luas. Bulatan + luas. Segiempat; cout << jum; } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR FP 201 PROGRAMMING FUNDAMENTALS Output :

19 #include <iostream. h> #define PI 3. 14 double luas. Bulatan; double luas. Segiempat;

19 #include <iostream. h> #define PI 3. 14 double luas. Bulatan; double luas. Segiempat; double jum; void kira. Luas 1(int) ; void kira. Luas 2(int, int) ; void Total(); void main() { double radius; cout << "enter radius: "; cin >> radius; kira. Luas 1(radius); kira. Luas 2(4, 5); Total(); } void kira. Luas 1(int r) { luas. Bulatan = PI * r; } void kira. Luas 2(int a, int b) { luas. Segiempat = a * b; } void Total() { jum = luas. Bulatan + luas. Segiempat; cout << jum; } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR FP 201 PROGRAMMING FUNDAMENTALS Output :

20 FP 201 PROGRAMMING FUNDAMENTALS FUNCTION CALL 1) 2) Call by value Call by

20 FP 201 PROGRAMMING FUNDAMENTALS FUNCTION CALL 1) 2) Call by value Call by reference © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

21 FP 201 PROGRAMMING FUNDAMENTALS Call By Value • When the function is called,

21 FP 201 PROGRAMMING FUNDAMENTALS Call By Value • When the function is called, the exact value will be passed to the function. © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

22 FP 201 PROGRAMMING FUNDAMENTALS Call By Value #include <iostream. h> int sum(int, int);

22 FP 201 PROGRAMMING FUNDAMENTALS Call By Value #include <iostream. h> int sum(int, int); void main ( ) { int x, y; double Sum; cout <<"enter two numbers: n" ; cin >>x>>y; Sum=sum(x, y); cout<<"Sum: "<<Sum; } int sum(int x, int y) {int Sum; Sum = x + y; return(Sum); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

23 FP 201 PROGRAMMING FUNDAMENTALS Call By Value #include <iostream. h> int sum(int, int);

23 FP 201 PROGRAMMING FUNDAMENTALS Call By Value #include <iostream. h> int sum(int, int); void main ( ) { int x, y; double Sum; cout <<"enter two numbers: n" ; cin >>x>>y; Sum=sum(x, y); Output : cout<<"Sum: "<<Sum; } int sum(int x, int y) {int Sum; Sum = x + y; return(Sum); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

24 FP 201 PROGRAMMING FUNDAMENTALS Call By Value #include <iostream. h> int sum(int, int);

24 FP 201 PROGRAMMING FUNDAMENTALS Call By Value #include <iostream. h> int sum(int, int); void main ( ) { int x, y; double Sum; cout <<"enter two numbers: n" ; cin >>x>>y; Sum=sum(x, y); Output : cout<<"Sum: "<<Sum; } int sum(int x, int y) {int Sum; Sum = x + y; return(Sum); } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

25 FP 201 PROGRAMMING FUNDAMENTALS Call By Reference • This method is using addressing

25 FP 201 PROGRAMMING FUNDAMENTALS Call By Reference • This method is using addressing or “&” symbol that is located in front of the variable. • Example : �&mark; • By using this method, the exact value is not passed instead the address of the value located will be passed. © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

26 FP 201 PROGRAMMING FUNDAMENTALS Call By Reference #include <iostream. h> void name(int, int&);

26 FP 201 PROGRAMMING FUNDAMENTALS Call By Reference #include <iostream. h> void name(int, int&); main ( ) { int x, y, sum; cout <<"enter two numbers: n" ; cin >>x>>y; name(x, y, sum); cout <<"n Sum = x + y = "<<sum; return 0; } void name(int x, int y, int &sum) { sum=x+y; } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR

27 FP 201 PROGRAMMING FUNDAMENTALS Call By Reference #include <iostream. h> void name(int, int&);

27 FP 201 PROGRAMMING FUNDAMENTALS Call By Reference #include <iostream. h> void name(int, int&); main ( ) { int x, y, sum; cout <<"enter two numbers: n" ; cin >>x>>y; name(x, y, sum); cout <<"n Sum = x + y = "<<sum; return 0; } void name(int x, int y, int &sum) { sum=x+y; } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR Output :

28 FP 201 PROGRAMMING FUNDAMENTALS Call By Reference #include <iostream. h> void name(int, int&);

28 FP 201 PROGRAMMING FUNDAMENTALS Call By Reference #include <iostream. h> void name(int, int&); main ( ) { int x, y, sum; cout <<"enter two numbers: n" ; cin >>x>>y; name(x, y, sum); cout <<"n Sum = x + y = "<< ∑ return 0; } void name(int x, int y, int &sum) { sum=x+y; } © 2011/2012 | PN NORHASLIZA BT MUHAMAD NOR Output :