Seoul National University

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.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
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 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.
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.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Basics II Lecture 2 Seoul National University Graphics & Media Lab.
 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
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.
 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
Functions.
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’
Functions and an Introduction to Recursion
Introduction to Classes
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 5 Classes.
FUNCTIONS IN C++.
Pointers and Pointer-Based Strings
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
Functions.
void Pointers Lesson xx
Dr. Shady Yehia Elmashad
6.12 Default Arguments.
4.9 Multiple-Subscripted Arrays
Functions Pass By Value Pass by Reference
Seoul National University
Variables have attributes
Functions and an Introduction to Recursion
Chapter 15 - C++ As A "Better C"
C++ Programming Lecture 17 Pointers – Part I
Pointers and Pointer-Based Strings
各題答對人數.
Seoul National University
Lecture 8 Object Oriented Programming (OOP)
Templates.
Presentation transcript:

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

Contents Function (7.1-7.6) Recursive function (7.3.3)

Introduction to Function How to define a simple function ? #include <iostream> void main() { const int height = 3, width = 5, length = 7; std::cout << “Volume is " << height*width*length; } #include <iostream> 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, 집중 해부 The data type of the returned value The name of the function The names of the parameters along with their data types int volume(int h, int w, int l) { return h*w*l; } The expression whose value is to be returned Indicates that a value is to be returned by the function

Function, a Means for Procedure Abstraction The function is a means for procedure abstraction. #include <iostream> 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; }

Default Arguments of a Function Default arguments allow some arguments to be omitted. Use default argument values which are expected to be used most of the time. #include <iostream> 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)

Inline Functions 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 <iostream> 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);

Argument Passing Mechanisms Call by value, call by reference, call by pointer #include <iostream> 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”; }

Argument Passing Mechanisms Call by value, call by reference, call by pointer #include <iostream> 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”; }

Recursive Functions A recursive function is a function which calls itself, either directly or indirectly. #include <iostream> 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 Functions