Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

Similar presentations


Presentation on theme: "CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)"— Presentation transcript:

1 CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)

2 C ONTENTS Introduction Library Functions

3 INTRODUCTION Functions are like building blocks They allow complicated programs to be divided into manageable pieces Some advantages of functions: A programmer can focus on just that part of the program and construct it, debug it, and perfect it Different people can work on different functions simultaneously Can be used in more than one place in a program or in different programs

4 INTRODUCTION There are two types of function : predefined functions : carry out tasks that have been preprogrammed in the C++ program user-defined functions : users can define how the output is produced

5 PREDEFINED FUNCTION/ LIBRARY FUNCTIONS Predefined functions are organized into separate libraries I/O functions are in iostream header Math functions are in math.h header Some of the predefined mathematical functions are: sqrt(x) pow(x,y) floor(x) In C++, predefined functions (library function) are organized into separate libraries

6 LIBRARY FUNCTION Function defined in math.h header FunctionDescriptionTypeExampleValue sqrt (x) squareroot  x doublesqrt(4.0)2.0 pow (x, y)power x y doublepow (2.0,3.0)8.0 fabs (x) absolute value for double double fabs(-3.5) fabs (3.5) 3.5 ceil (x) ceiling (round up) double ceil (3.1) ceil (3.8) 4.0 floor (x)floor (round down) doublefloor (3.1) floor (3.8) 3.0

7 Function defined stdlib.h header FunctionDescriptionExampleValue abs (x) absolute value for x, x an integer abs (-5) abs (5) 5555 labs (x) absolute value for x, x a long labs (-50000) labs (50000) 50000 rand()Any random number, integer type rand()any number LIBRARY FUNCTION

8 PREDEFINED FUNCTION # include void main() { int x; int a; double y=7.5; x=-70000; cout << "The result is "<<ceil(y)<<endl; cout << "The result is "<<floor(y)<<endl; cout << "The result is "<<labs(x)<<endl; //example of rand function for (a=1;a<=4;a++) cout << "The result of random num "<<a<<" is ”<<rand()<<endl; }

9 PREDEFINED FUNCTION Function define in string header :

10 PREDEFINED FUNCTION Example (strcpy and strcmp)

11 PREDEFINED FUNCTION Example (strlen) :

12 PREDEFINED FUNCTION Function define in ctype.h header : FunctionDescription tolower(x)returns the lowercase value of x toupper (x)returns the uppercase value of x isupper(x)determines if a character is uppercase. islower(x)determines if a character is lowercase isalpha(x)determines if a character is a letter (a-z, A-Z) isdigit(x)determines if a character is a digit (0-9)

13 PREDEFINED FUNCTION #include void main() {char input; cout<<"Enter any character: "; cin>>input; cout<<"The character you entered is : " << input <<endl; if (isalpha(input)) { cout<<"That 's an alphabetic character.\n"; if (islower(input)) { cout<<"The letter you entered is lowercase. \n"; input = toupper(input); cout<<"The uppercase letter is "<<input<<endl; } else { cout<<"The letter you entered is uppercase. \n"; input = tolower(input); cout<<"The lowercase letter is "<<input<<endl; } else cout<<"That's a numeric digit. \n"; }

14 Find the output 1.char company [50] = “Universiti Teknologi MARA” int length; length = strlen(company) cout<<”You entered “ <<length<< “character”; 2.char fname [10] = “Adam”, lname [10] = “Ahmad”; strcat(fname,lname); cout<<fname<<endl; cout<<lname<<endl; 3. char fname [10] = “Adam”, lname [10] = “Ahmad”; strcpy(fname,lname); cout<<fname<<endl; cout<<lname<<endl; EXERCISE

15 Write program to calculate and display the square values of the first ten REAL numbers. EXERCISE

16 ANSWER-OUTPUT 1.You entered 25 character 2.Adam Ahmad 3. Ahmad Ahmad

17 #include void main() { int square; for(int i=1;i<=10;i++) { square=pow(i,2); cout<<square<<endl; } ANSWER-OUTPUT


Download ppt "CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)"

Similar presentations


Ads by Google