predefined functions

INTRODUCTION TO
COMPUTER PROGRAMMING
(CSC425)
CHAPTER 5
FUNCTIONS
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
sqrt (x)
Description
squarerootx
Type
double
Example
sqrt(4.0)
Value
2.0
pow (x, y)
power xy
double
pow (2.0,3.0)
8.0
fabs (x)
absolute value
for double
ceiling (round
up)
floor (round
down)
double
fabs(-3.5)
fabs (3.5)
ceil (3.1)
ceil (3.8)
floor (3.1)
floor (3.8)
3.5
3.5
4.0
4.0
3.0
3.0
ceil (x)
floor (x)
double
double
LIBRARY FUNCTION
 Function
Function
abs (x)
labs (x)
rand()
defined stdlib.h header
Description
Example
Value
absolute value for x,
abs (-5)
5
x an integer
abs (5)
5
absolute value for x,
labs (-50000)
50000
x a long
labs (50000)
50000
Any random number,
rand()
any number
integer type
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
cout << "The
cout << "The
result is "<<ceil(y)<<endl;
result is "<<floor(y)<<endl;
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:
Function
Description
strcmp(string1, string2)
returns true if the two strings are
different; otherwise returns 0
strcpy(string1, string2)
assign the value of string2 into string1
strlen(string)
return the length of the string
strcat(string1,string2)
combine both string and assign it to
string 1
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";
}
EXERCISE
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
Write program to calculate and display the square
values of the first ten REAL numbers.
ANSWER-OUTPUT
1. You entered 25 character
2. Adam
Ahmad
3. Ahmad
Ahmad
ANSWER-OUTPUT
#include<iostream.h>
#include<math.h>
void main()
{
int square;
for(int i=1;i<=10;i++)
{
square=pow(i,2);
cout<<square<<endl;
}
}