Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Introduction to C++ Functions Chapter 6 Sections 6.1 – 6.3 Computer II.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
General Computer Science for Engineers CISC 106 Lecture 34 Dr. John Cavazos Computer and Information Sciences 05/13/2009.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Functions Modules in C++ are called functions and classes
Introduction to Methods
Chapter 6: Functions.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
CS102 Introduction to Computer Programming Chapter 6 Functions Part I.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
User defined functions
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Lecture 12: Functions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Introduction to Programming Lecture 40. Class Class is a user defined data type.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Methods CSCI 1301 What is a method? A method is a collection of statements that performs a specific task. Pre-Defined methods: available in Java library.
Methods.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
Class & Objects C++ offers another user-defined data type known class which is the most important feature of the object-oriented programming. A class can.
Lecture 12: Dividing Up Work. Why Using Functions Divide-and-conquer making large program development more manageable. Software reusability Use existing.
Functions + Overloading + Scope
Two or more functions can have the same name but different parameters
Object Oriented Systems Lecture 03 Method
This technique is Called “Divide and Conquer”.
Functions in C Mrs. Chitra M. Gaikwad.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
Introduction to Functions
Functions I Creating a programming with small logical units of code.
Value returning Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Programming
Functions, Part 1 of 3 Topics Using Predefined Functions
CS149D Elements of Computer Science
In C Programming Language
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CPS125.
Classes and Objects Systems Programming.
INTERFACES Explained By: Sarbjit Kaur. Lecturer, Department of Computer Application, PGG.C.G., Sector: 42, Chandigarh.
Presentation transcript:

Computer Programming II Lecture 4

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 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)..

Write a program that used to find the square of (n)number (using function).

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: