Revision.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
Exercise 2.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Functions Modules in C++ are called functions and classes
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
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.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
1 Original Source : and Problem and Problem Solving.ppt.
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++ 16 September 2008.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
Computer Engineering 2 nd Semester Dr. Rabie A. Ramadan 3.
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++ 16 September 2008.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
1 Structure of Simple C++ Program Chapter 1 09/09/13.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 1: An Overview of Computers and Programming Languages
Function Topic 4.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Chapter 1: An Overview of Computers and Programming Languages
Functions and an Introduction to Recursion
CO1401 Programming Design and Implementation
School of EECS, Peking University
Chapter 5 Classes.
Pointers and Pointer-Based Strings
Writing Reuseable Formulas
Global & Local Identifiers
Function Basics.
Lab 1 Introduction to C++.
Extra.
CSC1201: Programming Language 2
Name: Rubaisha Rajpoot
Chapter 1: An Overview of Computers and Programming Languages
Introduction to Programming
הרצאה 03 אבני היסוד של תוכנית ב- C
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Anatomy of a Function Part 1
Modular Programming with Functions
More ‘concepts’ on Function
File Review Declare the File Stream Object Name
CS1201: Programming Language 2
Lab 1 Introduction to C++.
Chapter 1: An Overview of Computers and Programming Languages
Namespaces How Shall I Name Thee?.
Modular Programming with Functions
Functions and an Introduction to Recursion
Pointers and Pointer-Based Strings
CSC1201: Programming Language 2
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CS1201: Programming Language 2
Anatomy of a Function Part 1
TOPIC: FUNCTION OVERLOADING
More ‘concepts’ on Function
Presentation transcript:

Revision

#include <iostream> using namespace std; class base { public:virtual void who() {cout << "Base\n";}}; class first_d : public base { public:void who() {cout << "First derivation\n";}}; class second_d : public base {};// who() not defined int main() {base base_obj, *p; first_d first_obj; second_d second_obj; p = &base_obj; p->who(); p = &first_obj; p = &second_obj; return 0; }

#include <iostream> using namespace std; class base { public:virtual void who() {cout << "Base\n";}}; class first_d : public base { public:void who() {cout << "First derivation\n";}}; // second_d now inherited first_d -- not base. class second_d : public first_d {};// who() not defined void main() {base base_obj,*p; first_d first_obj;second_d second_obj; p = &base_obj; p->who(); // access base's who() p = &first_obj; p->who(); // access first_d's who() p = &second_obj; p->who(); }

#include <iostream> using namespace std; class base { public:virtual void who() {cout << "Base\n";}}; class first_d : public base { public:void who() {cout << "First derivation\n";}}; // second_d now inherited first_d -- not base. class second_d : public first_d { Public: void who() {cout << "second derivation\n";}}; void main() {base base_obj,*p; first_d first_obj;second_d second_obj; p = &base_obj; p->who(); // access base's who() p = &first_obj; p->who(); // access first_d's who() p = &second_obj; p->who(); }

Functions Function Prototype Syntax return_type function_name ( [type [parameterName]]...); Function Definition Syntax return_type function_name ( [type parameterName]...) { statements; //function body }

#include <iostream> using namespace std; double FindArea(double length, double width); //function prototype void main() { double lengthOfYard; double widthOfYard; double areaOfYard; cout << "\nHow wide is your yard? "; cin >> widthOfYard; cout << "\nHow long is your yard? "; cin >> lengthOfYard; areaOfYard= FindArea(lengthOfYard,widthOfYard); cout<< "\nYour yard is " <<areaOfYard<< " square meter\n\n"; } double FindArea(double l, double w) { return l * w; }

#include <iostream> using namespace std ; void myFunc(); int x = 6; void main() { cout << "\n In main x is: " << x; { int x = 5; cout << "\n In main x is: " << x; myFunc(); cout << "\n Back in main, x is: " << x; } cout << "\n In main x is: " << x; } void myFunc() { int x = 8; cout << "\n In myFunc, local x: " << x << endl; { cout << "\n In block in myFunc, x is: " << x; int x = 9; cout << "\nVery local x: " << x; cout << "\nOut of block, in myFunc, x: " << x << endl;

Exercises Write the prototype for a function named Perimeter(), which returns int and that takes two parameters, both ints. Write the definition of the function Perimeter() The two parameters represent the length and width of a rectangle. Have the function return the perimeter (twice the length plus twice the width).

Overloading Functions C++ enables you to create more than one function with the same name. This is called function overloading. The functions must differ in their parameter list, with a different type of parameter, a different number of parameters, or both. Here's an example: int myFunction (int x, int y); int myFunction (long x, long y); int myFunction (long z); The return types can be the same or different on overloaded functions. You should note that two functions with the same name and parameter list, but different return types, generate a compiler error. long myFunction (int x, int y);

Exercises What is wrong with the following code? #include <iostream> using namespace std ; void myFunc(int x); int main() { int x, y; y = myFunc(6); cout << "x: " << x << " y: " << y << "\n"; } void myFunc(int x) return (4*x);

Exercises What is wrong with the following code? #include <iostream> using namespace std ; int myFunc( int x); int main() { int x, y; y = myFunc(x); cout << "x: " << x << " y: " << y << "\n"; } int myFunc(int x); return (4*x);