20
CHAPTER 7 FUNCTIONS Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 20802)

CHAPTER 7 fUNCTIONS

  • Upload
    odessa

  • View
    36

  • Download
    0

Embed Size (px)

DESCRIPTION

CHAPTER 7 fUNCTIONS. Instructor: Hidayah Elias Course: COMPUTER PROGRAMMING (BFC 20802). Think about this???. COMPANY ABC. COMPANY DEFG. Has departments Human Resource - handle staff performance - Salary Sales and Marketing - Manage company’s income Production - PowerPoint PPT Presentation

Citation preview

Page 1: CHAPTER 7 fUNCTIONS

CHAPTER 7FUNCTIONS

Instructor: Hidayah EliasCourse: COMPUTER PROGRAMMING (BFC 20802)

Page 2: CHAPTER 7 fUNCTIONS

Think about this???

COMPANY ABC

• Has no department• All the tasks is done by Admin Assistant

COMPANY DEFG

Has departments• Human Resource - handle staff performance - Salary • Sales and Marketing - Manage company’s income• Production - control product line

Page 3: CHAPTER 7 fUNCTIONS

7.1Modular Programming

Functions: a small program code that would do a specific task

Organization of Large Program : Separate tasks into functions

Page 4: CHAPTER 7 fUNCTIONS
Page 5: CHAPTER 7 fUNCTIONS

7.2 Type of Function

Math Functions – provided by C++ (#include<cmath>)cos(x), exp(x), fabs(x), floor(x), fmod(x,y), log(x), log10(x), pow(x,y), sin(x), sqrt(x), tan(x)

User-defined Function – designed by user/programmer Example:

void calculateBMI(double h, double w);void readHeightWeight();

Page 6: CHAPTER 7 fUNCTIONS

7.3Defining and Calling Functions

Function declaration/prototype: define functions that be used in a program

Function call: statement causes a function to execute

Function definition: statements that make up a functionFormat:

return_type  function_name(parameter list){            body of the function  }

Format: return_type  function_name(parameter list);Example: void printName();

int maxNumber(int num1, int num2);

Format: function_name(parameter list);Example: printName();

evenOdd(x); maxNumber(x,y);

void printName(){ char name[30]; cout<<"\nEnter name : "; cin.getline(name,30);}

Example:

Page 7: CHAPTER 7 fUNCTIONS

Function Definition Definition includes:

return type: data type of the value that function returns to the part of the program that called it

name: name of the function. Function names follow same rules as variables

parameter list: variables containing values passed to the function

body: statements that perform the function’s task, enclosed in {}Function with no return type : void

Page 8: CHAPTER 7 fUNCTIONS

Function Call

To execute a function, we need to call the function. Calling a function means transferring the control of the program to the beginning of the function.

evenOdd(x);

printName();

Function Name Passing Parameter/Value

Function call with no passing parameter/value

Page 9: CHAPTER 7 fUNCTIONS

Function Call

evenOdd(x); maxNumber(x,y);

Page 10: CHAPTER 7 fUNCTIONS

// function example#include <iostream> using namespace std;

int addition (int a, int b) { int r; r =a+b; return (r); }

int main () { int addition (int a, int b); int z; z = addition (5,3); cout << "The result is " << z; return 0; }

Passing value

Return value

Page 11: CHAPTER 7 fUNCTIONS

#include <iostream>using namespace std;

double calculateBMI(double h, double w);void readHeightWeight();

void main(){

readHeightWeight();cout<<"Thank You";system(“PAUSE”);

}

void readHeightWeight(){

double weight,height, BMI;cout<<"enter your height : “;cin>>height;cout<<"enter your weight: ";cin>>weight;BMI= calculateBMI(height, weight);cout<<“Your BMI is “<<BMI;

}

double calculateBMI(double h, double w){ double bmi; bmi=w/(h*h); return bmi;}

Page 12: CHAPTER 7 fUNCTIONS

#include<iostream>Using namespace std; void f1();void f2();void f3();

int main(){ f3(); cout<<“try… try”; f1(); cout<<“must try”; f2(); cout<<“good luck”; f3(); return 0;}

void f1(){cout<<“try again:”<<endl;}

void f2(){cout<<“already tried”<<endl;f1();}

void f3(){ cout<<“don’t try”<<endl;}

Page 13: CHAPTER 7 fUNCTIONS

#include<iostream>Using namespace std; int x;void f1();void f2();void f3();

int main(){x=10; f3(); f1(); f2(); f3(); cout<<x<<endl; return 0;}

void f1(){x=15;}

void f2(){x=50;}

void f3(){ x=25}

Show what the updated value of x for each line in main function

Page 14: CHAPTER 7 fUNCTIONS
Page 15: CHAPTER 7 fUNCTIONS

Flow of Control in Program 6-1

Page 16: CHAPTER 7 fUNCTIONS

(Program Continues)

Page 17: CHAPTER 7 fUNCTIONS

The function call in line 18 passes value1,value2, and value3 as a arguments to thefunction.

Page 18: CHAPTER 7 fUNCTIONS

7.7The return Statement

Used to end execution of a function Can be placed anywhere in a function

Statements that follow the return statement will not be executed

Can be used to prevent abnormal termination of program

In a void function without a return statement, the function ends at its last }

Page 19: CHAPTER 7 fUNCTIONS
Page 20: CHAPTER 7 fUNCTIONS

THANK YOU