SlideShare une entreprise Scribd logo
1  sur  33
Télécharger pour lire hors ligne
F2037 - PROGRAMMING
FUNDAMENTAL WITH C++
Unit 5.1 - Understand The Use Of Function
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
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
TYPES OF FUNCTIONS

   built-in function - system function.
   User-defined function - Created by the users for
    their requirements.
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)
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)<<"nn";
}
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;
}
Fp201 unit5 1
COMPONENTS OF FUNCTION
a)   Function header
b)   Function body
c)   Local variable
d)   Return statement
e)   Parameters declaration list
f)   Function prototypes
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.
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
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
Parameter list
 List of parameters that can be passed to the
  function
COMPONENTS OF FUNCTION
#include <iostream>
using namespace std;
int prt(int);               //Function prototype
int main()                  //Define function main()
{
   int x = 12;      //Declaration
   cout << prt(x);  //Calling function
   return 0;        //return 0 to situation
}
                     Parameter list
int prt(int y)          //Function header & Function definition
{
   return y;     //Return value         //Function body
}
Fp201 unit5 1
FUNCTION DECLARATION
 Syntax:

   function_type function_name (parameter-list)
   {
       variable declaration;
       ……
       return expression;
   }
FUNCTION DECLARATION

 function_type = type of data in C++
 function_name = name of function

 parameter-list  passing parameter value when invoked

 return  return value or control
IDENTIFY FUNCTION CALLS
1.   Call by value
2.   Call by reference
CALL BY VALUE
#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;
}
Fp201 unit5 1
CALL BY REFERENCE
#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;
}
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).
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.
#include<iostream>
using namespace std;
void kira_jumlah ( ); //fungsi prototype
void main ()
{

kira_jumlah ();

system(“pause”);
}
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<<“
}
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”);
}
void kira_jumlah ( int num_1, int num_2) )
{
int JUMLAH;

JUMLAH=num_1 + num_2;

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

}
Fp201 unit5 1
Fp201 unit5 1
Fp201 unit5 1

Contenu connexe

Tendances

An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1AmIt Prasad
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehmanNauman Rehman
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2Amit Kapoor
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings imtiazalijoono
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.Russell Childs
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Dharma Kshetri
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 
CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++Pranav Ghildiyal
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Ismar Silveira
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++NUST Stuff
 

Tendances (20)

This pointer
This pointerThis pointer
This pointer
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Pointers
PointersPointers
Pointers
 
Unit 3
Unit 3 Unit 3
Unit 3
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Learn c++ (functions) with nauman ur rehman
Learn  c++ (functions) with nauman ur rehmanLearn  c++ (functions) with nauman ur rehman
Learn c++ (functions) with nauman ur rehman
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)Maharishi University of Management (MSc Computer Science test questions)
Maharishi University of Management (MSc Computer Science test questions)
 
functions
functionsfunctions
functions
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
CBSE Class XI Programming in C++
CBSE Class XI Programming in C++CBSE Class XI Programming in C++
CBSE Class XI Programming in C++
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4Paradigmas de Linguagens de Programacao - Aula #4
Paradigmas de Linguagens de Programacao - Aula #4
 
C++ aptitude
C++ aptitudeC++ aptitude
C++ aptitude
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 

En vedette

En vedette (7)

Fp201 unit1 1
Fp201 unit1 1Fp201 unit1 1
Fp201 unit1 1
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2FP 201 Unit 2 - Part 2
FP 201 Unit 2 - Part 2
 
Unit 1
Unit 1Unit 1
Unit 1
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 2
Unit 2Unit 2
Unit 2
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 

Similaire à Fp201 unit5 1

Similaire à Fp201 unit5 1 (20)

Function C++
Function C++ Function C++
Function C++
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
C function
C functionC function
C function
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Function in c program
Function in c programFunction in c program
Function in c program
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Function
FunctionFunction
Function
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Basic information of function in cpu
Basic information of function in cpuBasic information of function in cpu
Basic information of function in cpu
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Preprocessor directives
Preprocessor directivesPreprocessor directives
Preprocessor directives
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 

Plus de rohassanie

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012rohassanie
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5rohassanie
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2rohassanie
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1rohassanie
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3rohassanie
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2) rohassanie
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201rohassanie
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201rohassanie
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012rohassanie
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1rohassanie
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 studrohassanie
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 studrohassanie
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3rohassanie
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2rohassanie
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1rohassanie
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++rohassanie
 

Plus de rohassanie (20)

Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012Course outline FP202 - Dis 2012
Course outline FP202 - Dis 2012
 
FP 202 - Chapter 5
FP 202 - Chapter 5FP 202 - Chapter 5
FP 202 - Chapter 5
 
Chapter 3 part 2
Chapter 3 part 2Chapter 3 part 2
Chapter 3 part 2
 
Chapter 3 part 1
Chapter 3 part 1Chapter 3 part 1
Chapter 3 part 1
 
FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3FP 202 Chapter 2 - Part 3
FP 202 Chapter 2 - Part 3
 
Lab ex 1
Lab ex 1Lab ex 1
Lab ex 1
 
Chapter 2 (Part 2)
Chapter 2 (Part 2) Chapter 2 (Part 2)
Chapter 2 (Part 2)
 
Labsheet 7 FP 201
Labsheet 7 FP 201Labsheet 7 FP 201
Labsheet 7 FP 201
 
Labsheet 6 - FP 201
Labsheet 6 - FP 201Labsheet 6 - FP 201
Labsheet 6 - FP 201
 
Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012Jadual Waktu Sesi JUN 2012
Jadual Waktu Sesi JUN 2012
 
Labsheet 5
Labsheet 5Labsheet 5
Labsheet 5
 
Labsheet 4
Labsheet 4Labsheet 4
Labsheet 4
 
Chapter 2 part 1
Chapter 2 part 1Chapter 2 part 1
Chapter 2 part 1
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Labsheet2 stud
Labsheet2 studLabsheet2 stud
Labsheet2 stud
 
Labsheet1 stud
Labsheet1 studLabsheet1 stud
Labsheet1 stud
 
Chapter 1 part 3
Chapter 1 part 3Chapter 1 part 3
Chapter 1 part 3
 
Chapter 1 part 2
Chapter 1 part 2Chapter 1 part 2
Chapter 1 part 2
 
Chapter 1 part 1
Chapter 1 part 1Chapter 1 part 1
Chapter 1 part 1
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 

Fp201 unit5 1

  • 1. F2037 - PROGRAMMING FUNDAMENTAL WITH C++ Unit 5.1 - Understand The Use Of Function
  • 2. 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
  • 3. 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
  • 4. TYPES OF FUNCTIONS  built-in function - system function.  User-defined function - Created by the users for their requirements.
  • 5. 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)
  • 6. 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)<<"nn"; }
  • 8. 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; }
  • 10. COMPONENTS OF FUNCTION a) Function header b) Function body c) Local variable d) Return statement e) Parameters declaration list f) Function prototypes
  • 11. 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.
  • 12. 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
  • 13. 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
  • 14. Parameter list  List of parameters that can be passed to the function
  • 15. COMPONENTS OF FUNCTION #include <iostream> using namespace std; int prt(int); //Function prototype int main() //Define function main() { int x = 12; //Declaration cout << prt(x); //Calling function return 0; //return 0 to situation } Parameter list int prt(int y) //Function header & Function definition { return y; //Return value //Function body }
  • 17. FUNCTION DECLARATION  Syntax: function_type function_name (parameter-list) { variable declaration; …… return expression; }
  • 18. FUNCTION DECLARATION  function_type = type of data in C++  function_name = name of function  parameter-list  passing parameter value when invoked  return  return value or control
  • 19. IDENTIFY FUNCTION CALLS 1. Call by value 2. Call by reference
  • 20. CALL BY VALUE #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; }
  • 22. CALL BY REFERENCE #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; }
  • 24. 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).
  • 26. 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.
  • 27. #include<iostream> using namespace std; void kira_jumlah ( ); //fungsi prototype void main () { kira_jumlah (); system(“pause”); }
  • 28. 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<<“ }
  • 29. 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”); }
  • 30. void kira_jumlah ( int num_1, int num_2) ) { int JUMLAH; JUMLAH=num_1 + num_2; cout<<“JUMLAH 2 integer adalah : “ << JUMLAH; }