Computer Programming II Lecture 2

advertisement
Computer Programming II
Lecture 2
Functions
- In C++ we use modules to divide the program into
smaller and manageable code. These modules are
called functions.
- A function is a collection of statements that performs
a specific task.
Functions
Defining and Calling Functions
- A function definition contains the statements that
make up the function.
- A function call is a statement that causes a function to
execute.
Functions
Function definition:
Return_type function_name (Parameter list)
{
Function body
}
Functions
All function definitions have the following parts:
1- Name: Every function must have a name. In general,
the same rules that apply to variable names also apply
to function names.
2- Parameter list : The parameter list is the list of
variables that hold the values being passed to the
function. If no values are being passed to the function,
its parameter list is empty.
Functions
3- Body : The body of a function is the set of
statements that carry out the task the function is
performing. These statements are enclosed in a set of
braces.
4- Return type : is the data type of the value the
function returns to the part of the program that
called it .
- If a function does not return a value, its return type is
void.
Types of functions
A-Function passes parameter and returns value.
B- Function passes parameter and doesn't return value.
C- Function doesn't pass parameter and returns value.
D- Function doesn't pass parameter and doesn't return
value.
Functions
Example :
void displaymessage()
{
cout << "Hello from the function displaymessage.\n";
}
Functions
Calling a Function
- A function is executed when it is called.
- When a function is called, the program branches to
that function and executes the statements in its body.
- To call a function, use the function name followed by ()
and ;
Functions
Example :
The function displaymessage is called by the following
statement :
displaymessage();
Example :
#include <iostream.h>
void displaymessage()
{
cout << "Hello from the function displaymessage.\n";
}
int main()
{
cout << "Hello from main.\n";
displaymessage(); // Call displaymessage
cout << "Back in function main again.\n";
return 0;
}
Functions
Function Prototypes
Prototype : is a line code showing the input and output
as well as the function name.
Note : Function prototypes are also known as function
declarations.
Examples:
void square (int x ) ;
int square (int y) ;
float square (float z) ;
char square ( char a );
This program is used to find the square of
(n)number (using function)..
This program is used to find the square of
(n)number (using function)..
Write a program that used to find the
square of (n)number (using function).
What is the output of the following program?
What is the output of the following program?
Write a program that used to find the sum
of two numbers using function.
Write a program that used to find the area
of rectangle using function.
Friend Function
• A friend function of a class is defined outside that class. It
has the right to access all private and protected members
of the class.
Friend Function
Syntax:
class class_name
{
..................
friendreturn_typefunction_name(argument/s);
..................
}

Friend Class
Syntax:
........ ..... ........
class A
{
friend class B; // class B is a friend class
..... ..... .....
}
class B
{
..... ..... .....
}

Friend Class

When a class is made a friend class, all the member
functions of that class becomes friend function.

In last example, all member functions of class B will be
friend function of class A. Thus, any member function of
class B can access the private and protected data of class
A.
Ex:
Download