Extra.

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

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.
Chapter 5 Functions.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Overview creating your own functions calling your own functions.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
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.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Chapter 5 Functions For All Subtasks. Void functions Do not return a value. Keyword void is used as the return type in the function prototype to show.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CS 240 Computer Programming 1
Dr. Shady Yehia Elmashad
Revision.
Chapter 5 Function Basics
Why exception handling in C++?
Functions and an Introduction to Recursion
CO1401 Programming Design and Implementation
Chapter 5 Functions.
FUNCTIONS IN C++.
Subject Name: PROGRAMMING IN C++ Subject Code: 10EC665
Pointers and Pointer-Based Strings
Writing Reuseable Formulas
Dr. Shady Yehia Elmashad
Dr. Shady Yehia Elmashad
CSC1201: Programming Language 2
Array of objects.
Chapter 5 Function Basics
CS150 Introduction to Computer Science 1
Pointers & Functions.
Anatomy of a Function Part 1
Array of objects.
Lab 1 Introduction to C++.
Pass by Reference.
Functions and an Introduction to Recursion
Introduction To Programming
Pointers and Pointer-Based Strings
CSC1201: Programming Language 2
CSC1201: Programming Language 2
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers and dynamic objects
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
CS1201: Programming Language 2
Anatomy of a Function Part 1
Presentation transcript:

Extra

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

Exercises Write a programme contain a function that takes two integer arguments and returns the result of dividing the first by the second. Do not do the division if the second number is zero, but do return -1. In main asks the user for two numbers and calls the function Print the answer, or print an error message if you get -1.

call by reference The call by reference method of passing arguments to a function, copies the reference of an argument into the formal parameter. Inside the function, the reference is used to access the actual argument used in the call. This means that changes made to the parameter affect the passed argument. void AddOne(int &y) { y = y + 1; }

#include <iostream> using namespace std; // function declaration void swap(int &x, int &y); void main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; } void swap(int &x, int &y) { int temp; temp = x; x = y; y = temp;

void chorus(Pet pt, Pet *petPtr, Pet &petRef) { pt.speak(); Trace the program. class Pet{public:void speak(){cout << "Growl" << endl;}}; class Rat: public Pet {public:void speak(){cout << "Rat noise" << endl;}}; class Cat: public Pet {Public:void speak(){cout << "Meow" << endl;}}; void chorus(Pet pt, Pet *petPtr, Pet &petRef) { pt.speak(); petPtr->speak(); petRef.speak();} void main() { Pet *ptr; //Pointer to base class ptr = new Pet; chorus(*ptr,ptr,*ptr); delete ptr; /////////////////////////////////////// ptr = new Rat; chorus(*ptr,ptr,*ptr); delete ptr; ////////////////////////////////////////// ptr = new Cat; chorus(*ptr,ptr,*ptr); delete ptr; }

Convert Speak() function to virtual function then trace the program again. class Pet {public: virtual void speak(){cout << "Growl" << endl;}};

Change class pet to be an abstract pet. class Pet{Public: virtual void speak()=0;}; What will you change in the main ??