1 / 20

CHAPTER 7 fUNCTIONS

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

odessa
Download Presentation

CHAPTER 7 fUNCTIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 7fUNCTIONS Instructor: Hidayah EliasCourse: COMPUTER PROGRAMMING (BFC 20802)

  2. Think about this??? COMPANY ABC COMPANY DEFG • Has departments • Human Resource • - handle staff performance • - Salary • Sales and Marketing • - Manage company’s income • Production • - control product line • Has no department • All the tasks is done by Admin Assistant

  3. 7.1 Modular Programming • Functions: a small program code that would do a specific task • Organization of Large Program : Separate tasks into functions

  4. 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();

  5. 7.3 Defining 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 function Format: return_typefunction_name(parameter list); Example: void printName(); intmaxNumber(int num1, int num2); Format: function_name(parameter list); Example: printName(); evenOdd(x); maxNumber(x,y); Example: void printName() { char name[30]; cout<<"\nEnter name : "; cin.getline(name,30); } Format: return_typefunction_name(parameter list) {             body of the function  }

  6. 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

  7. 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. Function Name Passing Parameter/Value evenOdd(x); Function call with no passing parameter/value printName();

  8. Function Call evenOdd(x); maxNumber(x,y);

  9. // 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

  10. #include <iostream> using namespace std; doublecalculateBMI(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; }

  11. #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; }

  12. #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

  13. Flow of Control in Program 6-1

  14. (Program Continues)

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

  16. 7.7 The 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 }

  17. THANK YOU

More Related