Tải bản đầy đủ (.ppt) (16 trang)

C++ lecture 13

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (302.42 KB, 16 trang )

C++ Programming
Lecture 13

Functions
– Part
V
The Hashemite
University
Computer Engineering
Department
(Adapted from the textbook slides)


Outline


Introduction.



Functions with empty parameter lists.



Inline functions.



Functions with default arguments.




Functions overloading.



Examples.

The Hashemite University

2


Functions with Empty
Parameter Lists


Empty parameter lists


Either writing void or leaving a parameter list empty
indicates that the function takes no arguments
void print();
or
void print( void );







Function print takes no arguments and returns no value
Calling a function that does not return any value inside cout
statement is syntax error (cout<Passing parameter to a function that does not take any
parameter is syntax error (print (5), or print (x) syntax
error)
The Hashemite University

3


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

20
21
22
23
24
25
26
27

// Fig. 3.18: fig03_18.cpp
// Functions that take no arguments
#include <iostream>
using std::cout;
using std::endl;
void function1();
void function2( void );

Notice the two ways of
declaring no arguments.

int main()
{
function1();
function2();
return 0;
}
void function1()
{
cout << "function1 takes no arguments" << endl;
}

void function2( void )
{
cout << "function2 also takes no arguments" << endl;
}

function1 takes no arguments
function2 also takes no arguments

The Hashemite University

4


Inline Functions


inline functions








Reduce function-call overhead
Asks the compiler to copy code into program
instead of using a function call
Compiler can ignore inline
Should be used with small, often-used functions


Example:
inline double cube( const double s )
{ return s * s * s; }

The Hashemite University

5


The Hashemite University

6


Default Arguments I






In the function prototype give all or some of the
arguments default values.
When you call the function, you can omit one or
more of the arguments values. The omitted
arguments will take their values from the default
values in the function prototype.
If function parameter omitted, gets default value










Can be constants, global variables, or function calls
If not enough parameters specified (in the calling statement),
rightmost go to their defaults
Default arguments in the prototype must be the right most
arguments or parameters for a function.
Can be used with inline functions also.

Set defaults in function prototype (only) where
the variables names are provided just for
readability.
int defaultFunction( int x = The
1, Hashemite
int y = University
2, int z = 3 );

7


Default Arguments II


In a function call you can omit the parameters that have default values only.




Not setting all the rightmost parameters after a default arguments to default is a syntax error.



E.g.:

int defaultFunction(int x = 1, int y, int z = 3);  Syntax error
int defaultFunction( int x = 1, int y, int z);  Syntax error

This means that no argument can take a default value unless the
one on its right has a default value
The Hashemite University

8


1

// Fig. 3.23: fig03_23.cpp

2

// Using default arguments

3

#include <iostream>


4
5

using std::cout;

6

using std::endl;

7
8

int boxVolume( int length = 1, int width = 1, int height = 1 );

9
10 int main()
11 {
12

cout << "The default box volume is: " << boxVolume()

13

<< "\n\nThe volume of a box with length 10,\n"

14

<< "width 1 and height 1 is: " << boxVolume( 10 )


15

<< "\n\nThe volume of a box with length 10,\n"

16

<< "width 5 and height 1 is: " << boxVolume( 10, 5 )

17

<< "\n\nThe volume of a box with length 10,\n"

18

<< "width 5 and height 2 is: " << boxVolume( 10, 5, 2 )

19

<< endl;

20
21

return 0;

22 }
23
24 // Calculate the volume of a box
25 int boxVolume( int length, int width, int height )
26 {

27
28 }

return length * width * height;

The Hashemite University

9


The default box volume is: 1
 
The volume of a box with length 10,
width 1 and height 1 is: 10
 
The volume of a box with length 10,
width 5 and height 1 is: 50
 
The volume of a box with length 10,
width 5 and height 2 is: 100

Notice how the rightmost
values are defaulted.

Program Output

The Hashemite University

10



Default arguments
example
#include<iostream>
using namespace std;
int fun1(int x,int y=6,int z=7);// correct prototype
void main()
{
cout<in the prototype
cout<cout<cout<}
int fun1(int x,int y,int z)
{
return x+y+z;
}

The Hashemite University

11


Function overloading
#include<iostream>
using namespace std;

#include<iostream>
using namespace std;


#include<iostream>
using namespace std;

#include<iostream>
using namespace std;

int fun1(int ,float
,char );
int fun1(int , float,
char);

int fun1(int ,float ,char );
float fun1(int , float, char);

int fun1(int ,float ,char );
int fun1(float, int , char);

int fun1(int ,float ,char );
int fun1(int , float);

int main()
{
cout<
int main()
{
cout<
int main()

{
cout<
return 0;
}

return 0;
}

return 0;
}

int fun1(int x1,float
y1,char z1)
{
return x1+y1+z1;
}

int fun1(int x1,float y1,char
z1)
{
return x1+y1+z1;
}

int fun1(int x1,float y1,char
z1)
{
return x1+y1+z1;
}


int fun1(int x1,float y1,char
z1)
{
return x1+y1+z1;
}

int fun1(int x,float y,char
z)
{
return x+y+z;
}

float fun1(int x,float y,char z)
{
return x+y+z;
}

int fun1(float y, int x,char z)
{
return x+y+z;
}

int fun1(int x,float y)
{
return x+y+z;
}

int main()
{
cout<

return 0;
}

Syntax error. Two
functions are the
same

Syntax error. changing
return type is not
enough

Parameters order is
different so it is ok

The Hashemite University

# of parameters is
different so it is ok
12


Function Overloading I











Function overloading means having functions with
same name and different parameters (different
number of parameters, or different data types, or
different order, or all of these issues at the
same time)
Goal: having functions with the same name but they
do different tasks
Most of the time overloaded functions perform
similar tasks ( i.e., a function to square ints,
and function to square floats).
int square( int x) {return x * x;}
float square(float x) { return x * x; }
Program chooses function by signature
 signature is determined by function name and
parameter types
The Hashemite University
13
Can have the same return
types


Function Overloading II


You cannot overload a function with default arguments with another version that takes no arguments 
syntax error.




Overloading a function with another version that have the same parameters numbers, types, and order
with just the return result data type is different is a syntax error.

The Hashemite University

14


1 // Fig. 3.25: fig03_25.cpp
2 // Using overloaded functions
3 #include <iostream>
4
5 using std::cout;

Functions have same name but
different parameters

6 using std::endl;
7
8 int square( int x ) { return x * x; }
9
10 double square( double y ) { return y * y; }
11
12 int main()
13 {
14

cout << "The square of integer 7 is " << square( 7 )


15

<< "\nThe square of double 7.5 is " << square( 7.5 )

16

<< endl;

17
18

return 0;

19 }

The square of integer 7 is 49
The square of double 7.5 is 56.25

The Hashemite University

15


Additional Notes


This lecture covers the following material from the textbook:




Fourth Edition


Chapter 3: Sections 3.15, 3.16, 3.18, 3.20,
3.21

The Hashemite University

16



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×