WITH C++ Prof. Amr Goneid AUC Part 5. Functions Prof. amr Goneid, - - PowerPoint PPT Presentation

with c
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1
  • Prof. amr Goneid, AUC

1

CSCE 110 PROGRAMMING FUNDAMENTALS

WITH C++

  • Prof. Amr Goneid

AUC Part 5. Functions

slide-2
SLIDE 2
  • Prof. amr Goneid, AUC

2

Functions

slide-3
SLIDE 3
  • Prof. amr Goneid, AUC

3

Functions

 Predefined (Library) Functions  Modular Programming with Functions  Types of Functions  Function Prototype Declaration  Function Definition  Formal & Actual Parameters  Who Sees Who: Scope of an Identifier  Parameter Passing

slide-4
SLIDE 4
  • Prof. amr Goneid, AUC

4

  • 1. Predefined Functions

Example:

#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;

slide-5
SLIDE 5
  • Prof. amr Goneid, AUC

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

  • Powers. Ex: pow(3.0,4.0) = 81

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

Output:

slide-6
SLIDE 6
  • Prof. amr Goneid, AUC

6

  • 2. Modular Programming with

Functions

Level

Main Function Function1 Function2 Function3 Function4 Function5 1 2 . .

Functions are natural building blocks for modular programming

slide-7
SLIDE 7
  • Prof. amr Goneid, AUC

7

C++ Program Structure

Compiler Directives

Function Prototypes int main ( ) { Main Data Declarations Main Actions } Used Functions Defined Here C++ program contains function prototype declarations and function definitions. The main is just another function.

slide-8
SLIDE 8
  • Prof. amr Goneid, AUC

8

Functions & The Main Function

A function is invoked by another function (e.g main)

int main( )

main Data Area Main Body

Invoke function Next action

Function Header

Local Data Area

Function Body

slide-9
SLIDE 9
  • Prof. amr Goneid, AUC

9

  • 3. Types of Functions

Input Params Input Params

Typed Function void Function

Action Output Params Returns a Single Scalar Value

slide-10
SLIDE 10
  • Prof. amr Goneid, AUC

10

  • 4. Function Prototype

Declaration

 Syntax:

<ftype> <fname> (formal parameter list) ;

 Examples: int cube ( int n ) ;

// A function receiving an int parameter (n) and returning an int value.

float minxy ( float x , float y ) ;

// A function receiving two float parameters ( x , y ) and returning a float value.

slide-11
SLIDE 11
  • Prof. amr Goneid, AUC

11

Prototype Declaration (Examples)

 void printchar ( char c , int n ) ;

// a function receiving two parameters ( c , n ) and returns nothing. It is supposed to do an action, e.g. print n of char c on one line.

 void errormessage ( ) ;

// a function receiving nothing and returning nothing. It is supposed to do an action, e.g. print a fixed error message.

slide-12
SLIDE 12
  • Prof. amr Goneid, AUC

12

  • 5. Function Definition

<ftype> <fname> ( List of Formal Parameters) { Local Data Declarations Function Actions (Executable Statements) }

slide-13
SLIDE 13
  • Prof. amr Goneid, AUC

13

Building Typed Functions

 Syntax:

<ftype> <fname> (formal params) { Local Data Area Function Body contains a statement: return < a value of type ftype > ; }

slide-14
SLIDE 14
  • Prof. amr Goneid, AUC

14

Example of an Integer Function

 Function to return the larger of two integer

numbers a and b.

int maxab ( int a , int b ) { //Does not need Local Data return ( (a >= b) ? a : b ); }

slide-15
SLIDE 15
  • Prof. amr Goneid, AUC

15

Example of a Real Function

 Function to return the area of a circle of radius r.

float area ( float r ) { // Local Data const float pi = 3.14159 ; // Action return ( pi * r * r ) ; }

slide-16
SLIDE 16
  • Prof. amr Goneid, AUC

16

Example of a Boolean Function

 Function to return true if an integer n is even and

false otherwise.

bool iseven ( int n ) { //Does not need Local Data return ( n % 2 == 0 ) ; }

slide-17
SLIDE 17
  • Prof. amr Goneid, AUC

17

Example of Using a Typed Function

// Prints if an entered integer is even or odd # include <iostream> using namespace std; // Function used…. bool iseven ( int n ); int main ( ) { int num; cout << “ Enter an integer number: “; cin >> num;

slide-18
SLIDE 18
  • Prof. amr Goneid, AUC

18

Example of Using a Typed Function

if ( iseven ( num ) ) cout << “ Number is even ! “ ; else cout << “ Number is odd ! “; return 0 ; } // Returns true if an integer is even, false otherwise bool iseven ( int n ) { return ( n % 2 == 0 ); }

slide-19
SLIDE 19
  • Prof. amr Goneid, AUC

19

Type of Returned Value

 Type of a value returned by a called

function must be consistent with the type expected by the caller as identified in the function prototype declaration.

slide-20
SLIDE 20
  • Prof. amr Goneid, AUC

20

Building void Functions

 Syntax:

void <fname> (formal params) { Local Data Area Function Body does not contain a return statement }

slide-21
SLIDE 21
  • Prof. amr Goneid, AUC

21

Example of a void Function

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

slide-22
SLIDE 22
  • Prof. amr Goneid, AUC

22

Example of a void Function

 Action: Write n dashes on a line.

void dashes( int n ) { const char dash = ‘-’ ; int i ; for (i = 1; i <= n; i++) cout << dash ; }

slide-23
SLIDE 23
  • Prof. amr Goneid, AUC

23

Example of Using a void Function

// Prints numbers and dashes # include <iostream> using namespace std; // Function used…. void dashes ( int n ); int main ( ) { float salary, bonus; cout << “Enter Salary: “; cin >> salary; bonus = 0.1 * salary ;

slide-24
SLIDE 24
  • Prof. amr Goneid, AUC

24

Example of Using a void Function

cout << “Bonus ” ; dashes(3); cout << bonus; dashes(5); cout << endl; return 0 ; } // Writes n dashes on one line

void dashes ( int n ) { const char dash = ‘-’ ; int i ; for (i = 1; i <= n; i++) cout << dash ; }

slide-25
SLIDE 25
  • Prof. amr Goneid, AUC

25

  • 6. Formal & Actual Parameters

In Function Declarations: bool iseven(int n); int maxab( int a , int b ) void dashes(int n); a,b,n are FORMAL parameters(Dummies or Gates). They are LOCAL to their modules. When invoked in a main function: maxab(x,y) or maxab(1+z,2.3) dashes(7); dashes(k); iseven ( num ) x , y , 1+z , 2.3 , 7 , k , num are ACTUAL parameters passed from main to modules through their respective gates.

slide-26
SLIDE 26
  • Prof. amr Goneid, AUC

26

Key Points

The substitution of the value of an

actual parameter in a function call for its corresponding formal parameter is strictly positional. That is, the value of the first actual parameter is substituted for the first formal parameter; the second and so on

slide-27
SLIDE 27
  • Prof. amr Goneid, AUC

27

Key Points

The names of these corresponding pairs of

parameters are no consequence in the substitution process. The names may be different, or they may be the same.

The substituted value is used in place of the

formal parameter at each point where that parameter appears in the called function.

slide-28
SLIDE 28
  • Prof. amr Goneid, AUC

28

Passing values of Actual Parameters

main maxab main iseven

a b n num x y

slide-29
SLIDE 29
  • Prof. amr Goneid, AUC

29

Formal & Actual Parameters

 Correspondence between actual and formal

parameters is determined by position in their respective lists. These lists must be the same

  • size. The names of corresponding actual and

formal parameters may be different.

 Formal parameters and corresponding actual

parameters should agree with respect to type.

slide-30
SLIDE 30
  • Prof. amr Goneid, AUC

30

Overloaded Functions:

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

slide-31
SLIDE 31
  • Prof. amr Goneid, AUC

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

Output:

slide-32
SLIDE 32
  • Prof. amr Goneid, AUC

32

  • 7. Who Sees Who:

Scope of an Identifier

 To see = to recognize = to be able to use,

invoke, change, etc.

 Scope = the domain in which an identifier is

recognizable.

 The scope of an identifier extends only from

the point where it is defined to the end of the module in which it is defined.

 A module can see itself (Recursion)

slide-33
SLIDE 33
  • Prof. amr Goneid, AUC

33

Scope(continued)

 Global : can be seen by all modules.  Local: can be seen only by its module but not by

  • ther modules.

 Names declared inside a function/main are local to

that function/main.

 Anything declared before the main function is global.

It can be called anywhere in the program.

 Hence, all functions are global.  For two things having the same id, local overrides

global.

slide-34
SLIDE 34
  • Prof. amr Goneid, AUC

34

Scope(example)

Module A

Data P , Q

Module B

Data x , w

Module C

Data m , n

Main

Data x , y

Prototypes of A , B , C int x , m; // Global Variables

slide-35
SLIDE 35
  • Prof. amr Goneid, AUC

35

 A function to swap two characters.

// x and y are passed by value void swap (char x , char y) { char temp; temp = x; x = y; y = temp; }

  • 8. Parameter Passing:

Example of a Paradox

slide-36
SLIDE 36
  • Prof. amr Goneid, AUC

36

Paradox (continued)

 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 ?

M N M N

slide-37
SLIDE 37
  • Prof. amr Goneid, AUC

37

Where in Memory?

 The DOS Memory Map:

  • ne segment = 64 kbyte

DS = Data Segment (Data) CS = Code Segment ( Main & Modules code) SS = Stack Segment (System Stack) Heap = Rest of DOS memory

DOS CS DS SS HEAP LM HM

slide-38
SLIDE 38
  • Prof. amr Goneid, AUC

38

Parameter Passing: What Really Happened

Memory Before Memory After

‘N’ ‘M’ ‘N’ ‘M’ a b a b

Swap addr

‘M’ -> x ‘N’-> y

Swap addr

‘N’ <- x ‘M’ <- y DS SS DS SS

slide-39
SLIDE 39
  • Prof. amr Goneid, AUC

39

To see the change, pass the address, not the value !

Memory Before Memory After

‘N’ ‘M’ ‘M’ ‘N’ a b a b

Swap addr

Addr of a Addr of b

Swap addr

Addr of a Addr of b DS SS DS SS

slide-40
SLIDE 40
  • Prof. amr Goneid, AUC

40

How to pass the Address (pass by Reference)

 The correct function to swap two characters.

// x and y are passed by reference void swap (char& x , char& y) { char temp; temp = x; x = y; y = temp; } // symbol & means address of

slide-41
SLIDE 41
  • Prof. amr Goneid, AUC

41

Passing by Reference (continued)

 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!

M N N M

slide-42
SLIDE 42
  • Prof. amr Goneid, AUC

42

Parameter Passing: Summary

 Input Only Parameters: those you do

not want to change- pass by value

 Output Only Parameters: those you

want to see what happened to them – pass by reference (address) using &.

 Input/Output Parameters: pass by

reference using &.