CHAPTER 5CHAPTER 5FUNCTIONSFUNCTIONS
INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER
PROGRAMMINGPROGRAMMING(CSC425)(CSC425)
CONTENTS
Introduction Library Functions
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
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
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
LIBRARY FUNCTION
Function defined in math.h header
Function Description Type Example Valuesqrt (x) squarerootx double sqrt(4.0) 2.0
pow (x, y) power xy double pow (2.0,3.0) 8.0
fabs (x) absolute value for double
double fabs(-3.5)fabs (3.5)
3.53.5
ceil (x) ceiling (round up)
double ceil (3.1)ceil (3.8)
4.04.0
floor (x) floor (round down)
double floor (3.1)floor (3.8)
3.03.0
Function defined stdlib.h header
Function Description Example Value
abs (x) absolute value for x,
x an integer
abs (-5)
abs (5)
5
5
labs (x) absolute value for x,
x a long
labs (-50000)
labs (50000)
50000
50000
rand() Any random number,
integer type
rand() any number
LIBRARY FUNCTION
PREDEFINED FUNCTION
#include <iostream.h> #include <stdlib.h> #include <math.h> 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;
}
PREDEFINED FUNCTION
Function define in string header:
PREDEFINED FUNCTION
Example (strcpy and strcmp)
PREDEFINED FUNCTION Example (strlen) :
PREDEFINED FUNCTION
Function define in ctype.h header:
Function Description
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)
PREDEFINED FUNCTION#include<iostream.h>#include<ctype.h>
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";}
Find the output1. 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
Write program to calculate and display the square values of the first ten REAL numbers.
EXERCISE
ANSWER-OUTPUT
1. You entered 25 character
2. Adam Ahmad
3. Ahmad Ahmad
#include<iostream.h>#include<math.h>
void main(){ int square;
for(int i=1;i<=10;i++) { square=pow(i,2); cout<<square<<endl; }}
ANSWER-OUTPUT