C++ Basics II Lecture 2 Seoul National University Graphics & Media Lab.

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

Procedures and Control Flow CS351 – Programming Paradigms.
Lecture 2 Aug goals: Introduction to recursion examples of recursive programs.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Practice 1 Seoul National University Graphics & Media Lab.
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.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
C++ Function 1. Function allow to structure programs in segment of code to perform individual tasks. In C++, a function is a group of statements that.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Lecture 2 Jan Goals: Introduction to recursion Examples of recursive programs Reading: Chapter 1 of the text.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
1 Lecture 3 More about C++. 2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static.
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
C++ Programming Lecture 17 Pointers – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Functions Sujana Jyothi C++ Workshop Day 2. Functions 3 Parameter transmission modes pass by value (default) pass by reference (&) pass by const reference.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
CSCI 62 Data Structures Dr. Joshua Stough December 2, 2008.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CS116 SENEM KUMOVA METİN. Outline What is C++ ? Some features of C++ Input and Output Operations Manipulators C++ header files Namespaces and scope resolution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
Chapter 15 - C++ As A "Better C"
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
Dr. Shady Yehia Elmashad
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
FUNCTIONS In C++.
Methods Attributes Method Modifiers ‘static’
Introduction to Classes
CSC113: Computer Programming (Theory = 03, Lab = 01)
Modern C++ - Bits & Pieces (Part I)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
Dr. Shady Yehia Elmashad
Introduction to Classes
Seoul National University
Variables have attributes
Chapter 15 - C++ As A "Better C"
C++ Programming Lecture 17 Pointers – Part I
Seoul National University
Lesson 25 Miscellaneous Topics
各題答對人數.
Seoul National University
Lecture 8 Object Oriented Programming (OOP)
Templates.
Preprocessor Directives and Macros Chapter 21
Presentation transcript:

C++ Basics II Lecture 2 Seoul National University Graphics & Media Lab

Contents Function ( ) Recursive function (7.3.3)

Function How to define a simple function ? #include void main() { const int height = 3, width = 5, length = 7; std::cout << " Volume is " << height*width*length; } #include int volume(int h, int w, int l) { return h*w*l; } void main() { const int height = 3, width = 5, length = 7; std::cout << " Volume is " << volume(height,width,length); }

Function int volume(int h, int w, int l) { return h*w*l; } The data type of the returned value The name of the function The names of the parameters along with their data types The expression whose value is to be returned Indicates that a value is to be returned by the function

Function The function is a means for procedure abstraction. #include int volume(int h, int w, int l) { return h*w*l; } int area(int h, int w, int l) { return 2*(h*w + w*l + l*h); } void main() { std::cout << "The volume of box1 is " << volume(3,5,7) << std::endl; std::cout << "The area of box1 is " << area(3,5,7) << std::endl; std::cout << "The area of box2 is " << area(10,20,30) << std::endl; std::cout << "The area of box3 is " << area(15,25,35) << std::endl; }

Function Default arguments – Default arguments allow some arguments to be omitted. – Use default argument values which are expected to be used most of the time. #include int get_area(int h, int w=10, int l=10) { return 2*(h*w + w*l + l*h); } void main() { std::cout << "The area is " << get_area(3) << std::endl; std::cout << "The area is " << get_area(3,11,12) << std::endl; } // get_area(3)  get_area(3,10,10) // get_area(3, 5)  get_area(3,5,10)

Function Inline functions – An inline function is expanded “in line” at each function call. – So there is no run-time overhead associated with the function call. #include inline int get_area(int h, int w, int l) { return 2*(h*w + w*l + l*h); } void main() { int h0=3, w0=5, l0=7; int area = get_area(h0,w0,l0); } // this line would be expanded during // compilation into something like int area = 2*(h0*w0 + w0*l0 + l0*h0);

Function Call by value, call by reference, call by pointer #include void swap_using_value(int a, int b) { int tmp = a; a = b; b = tmp; } void swap_using_ref(int& a, int& b) { int tmp = a; a = b; b = tmp; } void swap_using_ptr(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; } void main() { int u,v; u = 1, v = 2; swap_using_value(u,v); std::cout << u << " " << v << "\n"; u = 1, v = 2; swap_using_ref(u,v); std::cout << u << " " << v << "\n"; u = 1, v = 2; swap_using_ptr(&u,&v); std::cout << u << " " << v << "\n"; }

Function Call by value, call by reference, call by pointer #include void swap_using_value(int a, int b) { int tmp = a; a = b; b = tmp; } void swap_using_ref(int& a, int& b) { int tmp = a; a = b; b = tmp; } void swap_using_ptr(int* a, int* b) { int tmp = *a; *a = *b; *b = tmp; } void swap_using_const_ref(const int& a, const int& b) {int tmp=a; a=b; b=tmp;} void main() { int u,v; u = 1, v = 2; swap_using_value(u,v); std::cout << u << " " << v << "\n"; u = 1, v = 2; swap_using_ref(u,v); std::cout << u << " " << v << "\n"; u = 1, v = 2; swap_using_ptr(&u,&v); std::cout << u << " " << v << "\n"; u = 1, v = 2; swap_using_const_ref(u,v); std::cout << u << " " << v << "\n"; } // Compilation Error Occurs

Recursive function A recursive function is a function which calls itself, either directly or indirectly. #include int f(int n) { if(n==0 || n==1) return 1; else return f(n-1) + f(n-2); } void main() { std::cout << f(3) << std::endl; std::cout << f(10) << std::endl; }

Recursive function