chapter 5 functions i ntroduction t o c omputer p rogramming (csc425)

17
CHAPTER 5 CHAPTER 5 FUNCTIONS FUNCTIONS INTRODUCTION TO INTRODUCTION TO COMPUTER PROGRAMMING COMPUTER PROGRAMMING (CSC425) (CSC425)

Upload: isaac-gilmore

Post on 14-Dec-2015

231 views

Category:

Documents


3 download

TRANSCRIPT

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

CHAPTER 5CHAPTER 5FUNCTIONSFUNCTIONS

INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER

PROGRAMMINGPROGRAMMING(CSC425)(CSC425)

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

CONTENTS

Introduction Library Functions

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

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

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

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

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

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

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

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

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

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

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

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;

}

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

PREDEFINED FUNCTION

Function define in string header:

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

PREDEFINED FUNCTION

Example (strcpy and strcmp)

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

PREDEFINED FUNCTION Example (strlen) :

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

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)

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

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";}

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

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

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

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

EXERCISE

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

ANSWER-OUTPUT

1. You entered 25 character

2. Adam Ahmad

3. Ahmad Ahmad

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

#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