- Prof. amr Goneid, AUC
1
CSCE 110 PROGRAMMING FUNDAMENTALS
WITH C++
- Prof. Amr Goneid
WITH C++ Prof. Amr Goneid AUC Part 5. Functions Prof. amr Goneid, - - PowerPoint PPT Presentation
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 5. Functions Prof. amr Goneid, AUC 1 Functions Prof. amr Goneid, AUC 2 Functions Predefined (Library) Functions Modular Programming with Functions Types of
1
2
3
4
#include <math.h> void main() { cout << ”Square Root. Ex: sqrt(9.0) = ” << sqrt(9.0) << endl; cout << ”Powers. Ex: pow(3.0, 4.0) = ” << pow(3.0,4.0) << endl; cout << ”Absolute Value for int. Ex: ” << ”abs(-9) = ” << abs(-9) << endl; cout << ”Absolute Value for long. Ex: ” << ”labs(-900) = ” << labs(-900) << endl;
5
cout << ”Absolute Value for double. Ex: ” << ”fabs(-9.5) = ” << fabs(-9.5) << ”\n”; cout << ”Ceiling (round up). Ex: ceil(4.1)” << ” = ” << ceil(4.1) << endl; cout << ”Floor (round down). ” << ”Ex: floor(4.7) = ” << floor(4.7) << endl; } Square Root. Ex: sqrt(9.0) = 3.0
Absolute Value for int. Ex: abs(-9) = 9 Absolute Value for long. Ex: labs(-9000) = 9000 Absolute Value for double. Ex: fabs(-9.5) = 9.5 Ceiling (round up). Ex: ceil(4.1) = 5 Floor (round down). Ex: floor(4.7) = 4
6
Functions are natural building blocks for modular programming
7
Compiler Directives
8
int main( )
Invoke function Next action
Function Header
Function Body
9
Input Params Input Params
Action Output Params Returns a Single Scalar Value
10
// A function receiving an int parameter (n) and returning an int value.
// A function receiving two float parameters ( x , y ) and returning a float value.
11
void printchar ( char c , int n ) ;
void errormessage ( ) ;
12
13
14
Function to return the larger of two integer
15
Function to return the area of a circle of radius r.
16
Function to return true if an integer n is even and
17
18
19
20
21
Action: Fill screen with blanks.
void blankscreen( ) { const char blank = ‘ ’ ; int row , col ; for (row = 1; row <= 25; row++) { for (col = 1; col <= 80; col++) cout << blank ; cout << endl; } }
22
Action: Write n dashes on a line.
23
24
void dashes ( int n ) { const char dash = ‘-’ ; int i ; for (i = 1; i <= n; i++) cout << dash ; }
25
26
27
28
29
30
#include <iostream.h> float average(float x, float y); // Returns the average of x and y float average(float x, float y, float z); // Returns the average of x, y, and z void main() { cout << ”The average of 3.0 and 7.0” << ” is ” << average(3.0, 7.0) << endl; cout << ”The average of 3.0, 4.0, and 8.0” << ” is ” << average(3.0, 4.0,8.0) << endl; }
31
float average(float x, float y) { return ((x + y)/2.0); } float average(float x, float y, float z) { return ((x + y + z)/3.0); } The average of 3.0 and 7.0 is 5.0000 The average of 3.0, 4.0, and 8.0 is 5.0000
32
33
Global : can be seen by all modules. Local: can be seen only by its module but not by
Names declared inside a function/main are local to
Anything declared before the main function is global.
Hence, all functions are global. For two things having the same id, local overrides
34
Data P , Q
Data x , w
Data m , n
Data x , y
35
A function to swap two characters.
36
A program uses the function to swap two characters:
void swap (char x , char y); int main ( ) { char a,b ; a = ‘M’ ; b = ‘N’ ; cout << a << ‘ ‘ << b << endl; swap(a,b); cout << a << ‘ ‘ << b << endl; } No Change! Why ?
37
The DOS Memory Map:
DS = Data Segment (Data) CS = Code Segment ( Main & Modules code) SS = Stack Segment (System Stack) Heap = Rest of DOS memory
38
Swap addr
‘M’ -> x ‘N’-> y
Swap addr
‘N’ <- x ‘M’ <- y DS SS DS SS
39
Swap addr
Addr of a Addr of b
Swap addr
Addr of a Addr of b DS SS DS SS
40
The correct function to swap two characters.
41
A program uses the function to swap two characters:
void swap (char& x , char& y); int main ( ) { char a,b ; a = ‘M’ ; b = ‘N’ ; cout << a << ‘ ‘ << b << endl; swap(a,b); cout << a << ‘ ‘ << b << endl; } Now there is Change!
42