200 likes | 289 Views
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
E N D
CHAPTER 7fUNCTIONS Instructor: Hidayah EliasCourse: 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 • - control product line • Has no department • All the tasks is done by Admin Assistant
7.1 Modular Programming • Functions: a small program code that would do a specific task • Organization of Large Program : Separate tasks into 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();
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 }
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
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();
Function Call evenOdd(x); maxNumber(x,y);
// 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
#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; }
#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; }
#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
The function call in line 18 passes value1, value2, and value3 as a arguments to the function.
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 }