0% found this document useful (0 votes)
6 views15 pages

Lecture 56

Uploaded by

qyasirahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Lecture 56

Uploaded by

qyasirahmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Functions PRESENTED BY:

YASIR AHMAD,BAQIR RAZA AND MAJID ALI BAIG


Introduction

 Divide and conquer


 Construct a program from smaller pieces
or components
 Each piece more manageable than the
original program
Program Components in C++

 Programs written by
 combining new functions with “prepackaged” functions in
the C++ standard library.
 new classes with “prepackaged” classes.
 The standard library provides a rich collection of
functions.
 Functions are invoked by a function call
 A function call specifies the function name and provides
information (as arguments) that the called function needs
 Boss to worker analogy:
A boss (the calling function or caller) asks a worker (the
called function) to perform a task and return (i.e., report
back) the results when the task is done.
Program Components in C++

 Function definitions
 Only written once
 These statements are hidden from other
functions.
 Boss to worker analogy:
The boss does not know how the worker
gets the job done; he just wants it done
Functions

 Functions
 Allow the programmer to modularize a
program
 Local variables
 Known only in the function in which they are
defined
 All variables declared in function definitions
are local variables
 Parameters
 Local variables passed when the function is
called that provide the function with outside
information
Functions

 Why write functions?


 modularity
 re-use
 maintenance / testing
Function Definitions

 Create customized functions to


 Take in data
 Perform operations
 Return the result
 Format for function definition:
return-value-type function-name( parameter-list )
{
declarations and statements
}
 Example:
int square( int y)
{
return y * y;
}
1 // Fig. 3.3: fig03_03.cpp
2 // Creating and using a programmer-defined function
3 #include <iostream>
4 Notice how parameters and return
5 using std::cout; value are declared.
6 using std::endl;
7
8 int square( int ); // function prototype
9
10 int main()
11 {
12 for ( int x = 1; x <= 10; x++ )
13 cout << square( x ) << " ";
14
15 cout << endl;
16 return 0;
17 }
18
19 // Function definition
20 int square( int y )
21 {
22 return y * y;
23 }

1 4 9 16 25 36 49 64 81 100
1 // Fig. 3.4: fig03_04.cpp
2 // Finding the maximum of three integers
3 #include <iostream>
4
5 using std::cout;
6 using std::cin;
7 using std::endl;
8
9 int maximum( int, int, int ); // function prototype
10
11 int main()
12 {
13 int a, b, c;
14
15 cout << "Enter three integers: ";
16 cin >> a >> b >> c;
17
18 // a, b and c below are arguments to
19 // the maximum function call
20 cout << "Maximum is: " << maximum( a, b, c ) << endl;
21
22 return 0;
23 }
24
25 // Function maximum definition
26 // x, y and z below are parameters to
27 // the maximum function definition
28 int maximum( int x, int y, int z )
29 {
30 int max = x;
31
32 if ( y > max )
33 max = y;
34
35 if ( z > max )
36 max = z;
37
38 return max;
39 }

Enter three integers: 22 85 17


Maximum is: 85

Enter three integers: 92 35 14


Maximum is: 92

Enter three integers: 45 19 98


Maximum is: 98
Function Prototypes
 Function prototype
 Function name
 Parameters
 Information the function takes in
 C++ is “strongly typed” – error to pass a parameter of the wrong type
 Return type
 Type of information the function passes back to caller (default int)
 void signifies the function returns nothing
 Only needed if function definition comes after the function call in
the program
 Example:
int maximum( int, int, int );
 Takes in 3 ints
 Returns an int
References and Reference
Parameters
 Call by value
 Copy of data passed to function
 Changes to copy do not change original
 Used to prevent unwanted side effects
 Call by reference
 Function can directly access data
 Changes affect original
 Reference parameter alias for argument
 & is used to signify a reference
void change( int &variable )
{ variable += 3; }
 Adds 3 to the variable inputted
int y = &x.
 A change to y will now affect x as well
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
1 // Fig. 3.18: fig03_18.cpp
2 // Functions that take no arguments
3 #include <iostream>
4
5 using std::cout;
6 using std::endl;
7
8 void function1();
9 void function2( void );
10
11 int main()
12 {
13 function1();
14 function2();
15
16 return 0;
17 }
18
19 void function1()
20 {
21 cout << "function1 takes no arguments" << endl;
22 }
23
24 void function2( void )
25 {
26 cout << "function2 also takes no arguments" << endl;
27 }

function1 takes no arguments


function2 also takes no arguments
Function Overloading

 Function overloading
 Having functions with same name and different
parameters
 Should 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 determined by function name and
parameter types
 Can have the same return types

You might also like