33
F2037 - PROGRAMMING FUNDAMENTAL WITH C++ Unit 5.1 - Understand The Use Of Function

Fp201 unit5 1

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Fp201 unit5 1

F2037 - PROGRAMMING FUNDAMENTAL WITH C++Unit 5.1 - Understand The Use Of Function

Page 2: Fp201 unit5 1

LEARNING OUTCOME

At the end of this module, students should be able to:

Define a function Explain the following types of function:Built-in

function, User-defined function Describe the components of function : a. Function header b. Function body c. Local variable d. Return statement e. Parameters declaration list f. Function prototypes Identify the function calls : call by value, call by

reference Write program using functions

Page 3: Fp201 unit5 1

INTRODUCTION TO FUNCTION

A function is a set of statements that performs a specific task.

It can be called from any section in the program and executed.

The main advantage in using functions is that the number of lines in a program can be reduced.

One long program willl be divided into smaller parts and each of these parts is programmed separately

Page 4: Fp201 unit5 1

TYPES OF FUNCTIONS

built-in function - system function.

User-defined function - Created by the users for

their requirements.

Page 5: Fp201 unit5 1

Built-in Function User-defined Function

strlen(“fadzlina”) float purata(float x, float y)

atof(s) void panggilan(void)

pow(x, y) char response(char ch)

Page 6: Fp201 unit5 1

BUILT-IN FUNCTION#include<cmath>

#include<iostream>

using namespace std;

void main()

{

double d=100.0, d1=100.5, d2=22.0;

//find the square root of the number

cout<<"\nSquarer Root of" << d<<" is = "<<sqrt(d)<<endl;

//find log 10 of the number

cout<<"\nlog10 for" << d<<" is = "<<log10(d)<<endl;

//compute power of two number

cout<<"\n" << d<<" to the power of "<<d2;

cout<<" is = " <<pow(d,d2)<<"\n\n";

}

Page 7: Fp201 unit5 1
Page 8: Fp201 unit5 1

USER-DEFINED FUNCTION#include <iostream>

using namespace std;

void kira_luasSegiempat(int, int);

void main()

{

int p,l;

cout<<"Masukkan nilai panjang dan lebar: ";

cin>>p>>l;

kira_luasSegiempat(p,l);

}

void kira_luasSegiempat(int panjang, int lebar)

{

double luas;

luas = panjang * lebar;

cout<<"Luas segiempat :"<<luas <<endl;

}

Page 9: Fp201 unit5 1
Page 10: Fp201 unit5 1

COMPONENTS OF FUNCTION

a) Function headerb) Function bodyc) Local variabled) Return statemente) Parameters declaration listf) Function prototypes

Page 11: Fp201 unit5 1

COMPONENTS OF FUNCTION

Function Header Has three main parts

The name of the function The parameters of the function enclosed in

paranthesis Return value type

Function Body What ever is written with in { } in the above

example is the body of the function.

Page 12: Fp201 unit5 1

COMPONENTS OF FUNCTION

Function prototype The prototype of a function provides the basic

information about a function which tells the compiler that the function is used correctly or not. It contains the same information as the function header contains.

Function definition A function must be defined before it can be used

Page 13: Fp201 unit5 1

COMPONENTS OF FUNCTION

Local variable Variable declared inside main() or function

Call Function Call another function by passing value or reference

Return statement (return) Return the control back to the function that invoked

it

Page 14: Fp201 unit5 1

Parameter list List of parameters that can be passed to the

function

Page 15: Fp201 unit5 1

#include <iostream>using namespace std;int prt(int); //Function prototypeint main() //Define function main(){

int x = 12; //Declarationcout << prt(x);//Calling functionreturn 0; //return 0 to situation

}

int prt(int y) //Function header & Function definition{

return y; //Return value}

COMPONENTS OF FUNCTION

Parameter list

//Function body

Page 16: Fp201 unit5 1
Page 17: Fp201 unit5 1

FUNCTION DECLARATION Syntax:

function_type function_name (parameter-list)

{

variable declaration;

……

return expression;

}

Page 18: Fp201 unit5 1

function_type = type of data in C++ function_name = name of function parameter-list passing parameter value when invoked return return value or control

FUNCTION DECLARATION

Page 19: Fp201 unit5 1

IDENTIFY FUNCTION CALLS

1. Call by value2. Call by reference

Page 20: Fp201 unit5 1

#include <iostream>using namespace std;int main(){

void max_min(int, int);int x =2,y=3;

max_min(x,y);cout << "x: " << x;cout << "\ny: " << y;

return 0;}void max_min(int x, int y){

x= x*x;y= y*y;

}

CALL BY VALUE

Page 21: Fp201 unit5 1
Page 22: Fp201 unit5 1

#include <iostream>using namespace std;int main(){

void max_min(int&, int&);int x=8,y=1;

max_min(x,y);cout << "x: " << x;cout << "\ny: " << y;

return 0;}void max_min(int& x, int& y){

x= x*x;y= y*y;

}

CALL BY REFERENCE

Page 23: Fp201 unit5 1
Page 24: Fp201 unit5 1

IN CLASS EXERCISE

By using a function, write a program that able to receive two double numbers and calculate sum for that numbers (hint: use void function).

Page 25: Fp201 unit5 1
Page 26: Fp201 unit5 1

SUMMARY

Function is a component of the program written to perform a specific task.

By using functions, the code written once can be used many times anywhere in the program.

As the program is segregated into different smaller modules, it is very easy to correct the errors.

Page 27: Fp201 unit5 1

#include<iostream>using namespace std;void kira_jumlah ( ); //fungsi prototypevoid main (){

kira_jumlah ();

system(“pause”);}

Page 28: Fp201 unit5 1

void kira_jumlah ( ){int num_1, num_2, JUMLAH;

cout<<“Sila masukan 2 integer :”;cin>>num_1>>num_2;

JUMLAH=num_1 + num_2;

cout<<“}

Page 29: Fp201 unit5 1

FUNGSI YANG TIDAK MEMULANGKAN NILAI TETAPI MENERIMA NILAI

#include<iostream>

using namespace std;

void kira_jumlah ( int, int ); //fungsi prototype

void main ()

{

int num_1, num_2;

cout<<“Sila masukan 2 integer :”;

cin>>num_1>>num_2;

kira_jumlah (num_1,num_2);

system(“pause”);

}

Page 30: Fp201 unit5 1

void kira_jumlah ( int num_1, int num_2) ){int JUMLAH;

JUMLAH=num_1 + num_2;

cout<<“JUMLAH 2 integer adalah : “ << JUMLAH;

}

Page 31: Fp201 unit5 1
Page 32: Fp201 unit5 1
Page 33: Fp201 unit5 1