School of EECS, Peking University

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 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
True or false A variable of type char can hold the value 301. ( F )
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Functions Modules in C++ are called functions and classes
Chapter 7 Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
GE 211 Function in C++ Dr. Ahmed Telba Math Library Functions Defining a Function: The general form of a C++ function definition is as follows:
Parameter Passing Mechanisms Reference Parameters Read § §
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
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.
Parameter Passing Mechanisms Reference Parameters § §
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
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.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
GE 211 Dr. Ahmed Telba. // compound assignment operators #include using namespace std; int main () { a =5 int a, b=3; a = b; a+=2; // equivalent to a=a+2.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
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
Course Title Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 06 FUNCTIONS 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER) 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
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.
Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Data Type and Function Prepared for CSB210 Pemrograman Berorientasi Objek By Indriani Noor Hapsari, ST, MT Source: study.
Chapter 7: User-Defined Functions II
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Exercise 1 – Datentypen & Variablen
IS Program Design and Software Tools Introduction to C++ Programming
C Functions -Continue…-.
Basic Elements of C++.
Functions and an Introduction to Recursion
FUNCTIONS IN C++.
Basic Elements of C++ Chapter 2.
Introduction to Functions
User Defined 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.
Name: Rubaisha Rajpoot
Review for Final Exam.
CS150 Introduction to Computer Science 1
Pointers & Functions.
Chapter 6: User-Defined Functions I
Review for Final Exam.
C++ Language Tutorial Second Class First Semester Lecturers:
Functions and an Introduction to Recursion
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CS1201: Programming Language 2
Functions Chapter No. 5.
Presentation transcript:

School of EECS, Peking University Functions http://net.pku.edu.cn/~course/cs101/2008 Hongfei Yan School of EECS, Peking University 11/12/2008

Outline function definition and declaration Scope of variables Arguments passed Default values in parameters, overloaded functions, and function templates Recursivity

Function definition type name ( parameter1, parameter2, ...) { statements } where: type is the data type specifier of the data returned by the function. name is the identifier by which it will be possible to call the function. parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas. statements is the function's body. It is a block of statements surrounded by braces { }. http://net.pku.edu.cn/~course/cs101/resource/www.cplusplus.com/doc/tutorial/functions.html functions we can structure our programs in a more modular way

#include <iostream> using namespace std; int addition (int a, int b) { int r; r=a+b; return (r); } int main () int z; z = addition (5,3); cout << "The result is " << z; The result is 8

The parameters and arguments have a clear correspondence At the point at which the function is called from within main, the control is lost by main and passed to function addition. The value of both arguments passed in the call (5 and 3) are copied to the local variables int a and int b within the function.

finalizes function addition The content of z becomes the value of evaluating the function call. returns the control back to the function that called it in the first place (in this case, main). At this moment the program follows it regular course from the same point at which it was interrupted by the call to addition.

Another example about functions #include <iostream> using namespace std; int subtraction (int a, int b) { int r; r=a-b; return (r); } int main () int x=5, y=3, z; z = subtraction (7,2); cout << "The first result is " << z << '\n'; cout << "The second result is " << subtraction (7,2) << '\n'; cout << "The third result is " << subtraction (x,y) << '\n'; z= 4 + subtraction (x,y); cout << "The fourth result is " << z << '\n'; The first result is 5 The second result is 5 The third result is 2 The fourth result is 6

Functions with no type (the use of void) #include <iostream> using namespace std; void printmessage () { cout << "I'm a function!"; } int main () printmessage (); I'm a function! Imagine that we want to make a function just to show a message on the screen. We do not need it to return any value. In this case we should use the void type specifier for the function. This is a special specifier that indicates absence of type.

Declaring a prototype of the function before it is used type name ( argument_type1, argument_type2, ...); The parameter enumeration does not need to include the identifiers, but only the type specifiers. The inclusion of a name for each parameter as in the function definition is optional in the prototype declaration. int protofunction (int first, int second); int protofunction (int, int);

Type a number (0 to exit): 9 Number is odd. #include <iostream> using namespace std; void odd (int a); void even (int a); int main () { int i; do { cout << "Type a number (0 to exit): "; cin >> i; odd (i); } while (i!=0); } void odd (int a) if ((a%2)!=0) cout << "Number is odd.\n"; else even (a); void even (int a) if ((a%2)==0) cout << "Number is even.\n"; else odd (a); Type a number (0 to exit): 9 Number is odd. Type a number (0 to exit): 6 Number is even. Type a number (0 to exit): 1030 Type a number (0 to exit): 0

POJ 2798 binary notation to hexadecimal notation #include <iostream> #include <string> using namespace std; const char table[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; char change(string w,int l) { int r=0; for (int i=0; i<l;i++) r = r*2 + w[i] - '0'; return table[r]; } int main() int n; cin >> n; while (n--) { string num; cin >> num; int len=num.size(); string subnum,result; result.clear(); while (len>=4) { subnum = num.substr(len-4,4); // Generate substring result = change(subnum,4)+result; len -= 4; if (len) { subnum = num.substr(0,len); result = change(subnum,len)+result; cout << result <<endl; Const keyword enables defining of a constant- a variable that cannot be reassigned a new value after initialization. http://net.pku.edu.cn/~course/cs101/2008/resource/cplusplus.com_20081105/reference/string/string/substr.html

Outline function definition and declaration Scope of variables Arguments passed Default values in parameters, overloaded functions, and function templates Recursivity

Scope of variables The scope of variables declared within a function or any other inner block cannot be used outside of them.

Arguments passed By value By reference what we have passed to the function were copies of their values but never the variables themselves. By reference we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function.

passing parameters by reference /#include <iostream> using namespace std; void duplicate (int& a, int& b, int& c) { a <<= 1; b <<= 1; c <<= 1; } int main () int x=1, y=3, z=7; duplicate (x, y, z); cout << "x=" << x << ", y=" << y << ", z=" << z; the type of each parameter was followed by an ampersand sign (&). x=2, y=6, z=14

A function to return more than one value #include <iostream> using namespace std; void prevnext (int x, int& prev, int& next) { prev = x-1; next = x+1; } int main () int x=100, y, z; prevnext (x, y, z); cout << "Previous=" << y << ", Next=" << z; Previous=99, Next=101

Outline Functions function definition and declaration Scope of variables Arguments passed Default values in parameters, overloaded functions, and function templates Recursivity

Default values in functions #include <iostream> using namespace std; int divide (int a, int b=2) { int r; r=a/b; return (r); } int main () cout << divide (12); cout << endl; cout << divide (20,4); If a value for that parameter is not passed when the function is called, the default value is used, but if a value is specified this default value is ignored and the passed value is used instead.

Overloaded function two different functions can have the same name #include <iostream> using namespace std; int operate (int a, int b) { return (a*b); } float operate (float a, float b) return (a/b); int main () int x=5,y=2; float n=5.0,m=2.0; cout << operate (x,y); cout << "\n"; cout << operate (n,m); two different functions can have the same name if their parameter types or number are different.

Function templates #include <iostream> using namespace std; template <class T> T operate (T a, T b) { return (a/b); } int main () int x=5,y=2; float n=5.0,m=2.0; cout << operate (x,y); cout << "\n"; cout << operate (n,m); Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type.

Outline function definition and declaration Scope of variables Arguments passed Default values in parameters, overloaded functions, and function templates Recursivity

Recursion #include <iostream> using namespace std; long long factorial (long long a) { if (a > 1) return (a * factorial (a-1)); else return (1); } int main () long long number; //cout << sizeof(long long) << endl; cout << "Please type a number: "; cin >> number; cout << number << "! = " << factorial (number); Recursion is the property that functions have to be called by themselves. It is useful for many tasks, like sorting or calculate the factorial of numbers.

references Functions: http://net.pku.edu.cn/~course/cs101/2008/resource/cplusplus.com_20081105/doc/tutorial/functions.html http://net.pku.edu.cn/~course/cs101/2008/resource/cplusplus.com_20081105/doc/tutorial/functions2.html