Download presentation
Presentation is loading. Please wait.
Published byGervais Hopkins Modified over 8 years ago
1
Computer Programming II Lecture 4
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.
3
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.
4
Functions Function definition: Return_type function_name (Parameter list) { Function body }
5
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.
6
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.
7
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.
8
Functions Example : void displaymessage() { cout << "Hello from the function displaymessage.\n"; }
9
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 ;
10
Functions Example : The function displaymessage is called by the following statement : displaymessage();
11
Example : #include 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; }
12
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 );
13
This program is used to find the square of (n)number (using function)..
15
Write a program that used to find the square of (n)number (using function).
16
What is the output of the following program?
18
Write a program that used to find the sum of two numbers using function.
19
Write a program that used to find the area of rectangle using function.
20
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.
21
Friend Function Syntax: class class_name {.................. friendreturn_typefunction_name(argument/s);.................. }
22
Friend Class Syntax:..................... class A { friend class B; // class B is a friend class............... } class B {............... }
23
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.
24
Ex:
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.