Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
The Art of Design Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Introduction to Computers and Programming Introduction to Methods in Java.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
Control Statements (II) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
1 Array, Pointer and Reference ( I ) Ying Wu Electrical Engineering and Computer Science Northwestern University EECS 230 Lectures.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
 2000 Prentice Hall, Inc. All rights reserved. Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
1 Lecture 3 Part 1 Functions with math and randomness.
16&17-2 Grasp the concept of top-down programming Identify Function Headers and Prototypes Understand when and where prototypes used Understand how arguments.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Functions in C Programming Dr. Ahmed Telba. If else // if #include using namespace std; int main() { unsigned short dnum ; cout
Array, Pointer and Reference ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 6 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions Course conducted by: Md.Raihan ul Masood
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
IS Program Design and Software Tools Introduction to C++ Programming
Chapter 3 - Functions Outline 3.1 Introduction
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Deitel- C:How to Program (5ed)
Dr. Shady Yehia Elmashad
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
Chapter 5 - Functions Outline 5.1 Introduction
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
توابع در C++ قسمت اول اصول كامپيوتر 1.
Chapter 6 - Functions Outline 5.1 Introduction
Value returning Functions
Presentation transcript:

Function ( I ) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series

The GOAL for Week-3/4 Understand arrays Fully understand addressing Understand pointers (angel vs. devil) Understand references Master C/C++ control structures Fully understand the use of functions Fully understand call-by-value and call-by- reference Start modular design DEBUG_2: Basic skills: dump results Do the MP#2: A Command Line Interpreter

MP#2: Command Interpreter MP#2 description MP#2 demo So, to do that, what shall we learn? Make small “tools” or building blocks? Modular design? Put modules together? Find bug?

What to learn today? What motivates to use “function calls”? What is a function call? What is local variable? What is the return of a function call? What is exactly happening when invoking a function call?

Motivation … When you were doing your MP#1, you might have typed many times the following… if ( (buffer[st]>=‘a’ && buffer[st] =‘A’ && buffer[st]<=‘Z’) ){.. } Or char buffer[500], piece[100]; for(int i=st; i<=ed; i++) piece[i] = buffer[i+st]; piece[ed-st+1] = ‘\0’; This way, your code may look lengthy and messy  You may wonder: why should I type the same thing so many times? Is there a way to write it once, and use it whenever I want?

Motivation … Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program Boss/worker and blackbox –A boss asks a worker to perform a task and return (i.e., report back) the results when the task is done. –The boss does not care how the task is done, but only concerning about “inputs” and “outputs”. So, everything the worker has done is a blackbox to the boss –E.g., get the sqrt of a number! Can my program has such a mechanism?

Good News! Functions are invoked by a function call –Analogy: “Boss & worker” The boss --- the calling function or caller The worker --- the called function The boss does not need to care about how the worker gets the job done The boss just wants it done –A function call specifies the function name and provides information (as arguments) that the called function needs Function definitions –Only written once –These statements are hidden from other functions. function( ) input output

Think before coding! If I want to check if or not a char is a letter: –Input? –Output? If I want to copy a string to another char array: –Input? –Output? If I want to find a “segment” in a command line: –Input? –Output?

How to define a function? Create customized functions to –Take in data –Perform operations –Return the result Format for function definition: return-value-type function-name( parameter-list ) { declarations and statements; } Example : int square( int y) { return y * y; }

1. Function prototype 2. Loop 3.Function definition Program Output 1// Fig. 3.3: fig03_03.cpp 2// Creating and using a programmer-defined function 3#include 4 5using std::cout; 6using std::endl; 7 8int square( int ); // function prototype 9 10int main() 11{ 12 for ( int x = 1; x <= 10; x++ ) 13 cout << square( x ) << " "; cout << endl; 16 return 0; 17} 18 19// Function definition 20int square( int y ) 21{ 22 return y * y; 23} Notice how parameters and return value are declared.

1. Function prototype (3 parameters) 2. Input values 2.1 Call function 1// Fig. 3.4: fig03_04.cpp 2// Finding the maximum of three integers 3#include 4 5using std::cout; 6using std::cin; 7using std::endl; 8 9int maximum( int, int, int ); // function prototype 10 11int main() 12{ 13 int a, b, c; cout << "Enter three integers: "; 16 cin >> a >> b >> c; // a, b and c below are arguments to 19 // the maximum function call 20 cout << "Maximum is: " << maximum( a, b, c ) << endl;

3.Function definition Program Output return 0; 23} 24 25// Function maximum definition 26// x, y and z below are parameters to 27// the maximum function definition 28int maximum( int x, int y, int z ) 29{ 30 int max = x; if ( y > max ) 33 max = y; if ( z > max ) 36 max = z; return max; 39} Enter three integers: Maximum is: 85 Enter three integers: Maximum is: 92 Enter three integers: Maximum is: 98

What can be in the inputs? The inputs of the function call is specified in the argument list (NOTE) the argument list can also be used to get the output through call-by-reference (you’ll learn that later) The argument list can have anything you want it there.

What can be a return? Unlike Matlab, C/C++ function only allows ONE return, which can be –A variable, e.g., int, double, bool… –A pointer, e.g., int*, double*, … –A reference, e.g., int&, double&, … –void, i.e., nothing

Function Prototypes Function prototype –Function name –Parameters Information the function takes in –Return type Type of information the function passes back to caller (default int ) void signifies the function returns nothing –Only needed if function definition comes after the function call in the program Example: int maximum( int, int, int ); –Takes in three int s –Returns an int

Math Library Functions Math library functions –Allow the programmer to perform common mathematical calculations.used > Example cout << sqrt( ); –Calls the sqrt (square root) function. The preceding statement would print 30 –double sqrt(double x); Function arguments can be –Constant sqrt( 4 ); –Variable sqrt( x ); –Expressions sqrt( sqrt( x ) ) ; sqrt( 3 - 6x );

Header Files Header files –Contain function prototypes for library functions –,, etc. –Load with #include Example: #include Custom header files –Defined by the programmer –Save as filename.h –Loaded into program using #include "filename.h"

Local Variables Local variables –Known only in the function in which they are defined –All variables declared in function definitions are local var. –The LIFE CYCLE of a local variable is only in the function in which it is used. Parameters –Local variables passed when the function is called that provide the function with outside information void main() { int x = 20; int y = myfunc(x); } int myfunc(t) { int x; x = t+1; return x; }

To have a deep understanding… Let’s understand the following: –Your program  machine code –Loading … –Instruction Pointer (IP) –Stack –Invoking a function call

Compiling and Linking Machine object code, e.g., a.obj Compiler Linker Various libraries Machine executable code (the stuff for machine to execute, not for you.  ) e.g., “a.exe” Your C/C++ source code (the stuff you wrote) e.g., a.cpp

Loading … Load.exe to memory (when you execute.exe) main( ) int func(a,b) func(a,b) x=func(3,4); return c; Code seg.

Instruction Pointer How does the computer execute the program? main( ) int func(a,b) func(a,b) x=func(3,4); return c; I. P. Code seg. Let me demonstrate this process in VC++ debugger! Question: when a function is called, for the I.P., (1)where to go? (2)how to get back from function call?

A Concept: Stack (LIFO) push(20) push(34) push(12) pop(12) 20 pop(34) pop(20)

Invoking a Function What happens when a function is invoked? –where is my code? –what happens to i.p.? –what happens to stack? –how to pass parameters? –how to get the return? c main( ) int func(a,b) func(a,b) x=func(3,4); return c; I. P. Code seg. stack

Question for Today! int fun(int x) { int y; y = x + 2; return y; } void main( ) { int y = 5; int x = 1; x = fun(x); } Question: after main() is executed, x=?, y=?

Prepare for MP#2 Take a look at my solution to MP#1, figure out which part can be replaced by function calls.

About MP#2 Attention, we only check 4 types of valid grammars. 1.assignment: "operand_1 = operand_2", e.g., a = 1 2.binary operation: "operand_1 operator operand_2", e.g., a + b 3.unary operation: "operand_1 operator", e.g., a++ 4.assignment and binary operation: "result = operand_1 operator operand_2", e.g., a = b + c According to that, we can define –Operations, (e.g., +, -, *, /, ++, --) –Operation types. (e.g., assignment, unary, binary) So, enum OP {ASN, ADD, MIN, MUL, DIV, INC, DEC}; enum OP_TYPE {asn, unary, binary};

A trick to do MP#2 You may want to find the # of segments of the command line –Since we only allow 2, 3, 5 Observe the position of operators (if defining a bool pIsOP[5] = {false, false, false, false, false}. –a + b{F, T, F, F, F} –c ++{F, T, F, F, F} –a = 1{F, T, F, F, F} –a = b + 1{F, T, F, T, F} From this observation, we only allow two patterns: –{F, T, F, F, F} and {F, T, F, T, F}