Functions Definition and Use Copyright 2006 2015 by
Functions Definition and Use Copyright © 2006 -2015 by Curt Hill
Preview • In this section we will learn or review about the following concepts: • Definition of functions – Aka user defined functions • Parameter passage mechanisms – By value – By reference • Declaration of parameters – FORTRAN style and Pascal style • Scope of variables – Global and local variables Copyright © 2006 -2015 by Curt Hill
Methods • A method is a function that wholly belongs to a class • With very few exceptions there are no other differences between a function and method • C has only functions • C++ has both functions and methods • Java has only methods • Next we review what we already know and have seen Copyright © 2006 -2015 by Curt Hill
main function • The only function definition that we have seen defined is the main function • It often takes a void and returns an int • This int is used by the operating system, in ways that we currently do not care about • The fact that the main function takes a void and returns an int does not limit it from I/O • Hence when we talk about input and output, we may mean parameters passed or file activity Copyright © 2006 -2015 by Curt Hill
Methods • Dev event handlers are all methods • They are usually void functions – They return no value • The parameters are a command type • However, since the method header has been generated for us, we have paid little attention to it Copyright © 2006 -2015 by Curt Hill
Operators • Conceptually our operators are functions • Consider the + operator – Take the item that is on the left and add it to the item on the right and return a result • A black box that has one or more input slots and returns one value Copyright © 2006 -2015 by Curt Hill
Form of a function call • In C++ there actually two forms of function call – C style function calls – Operator overloads • The C style function call form is the function name followed by parentheses surrounding parameters – This is usually part of a statement Copyright © 2006 -2015 by Curt Hill
Example • Consider: x = stuff*pow (a, b*2)-things; • The name of the function is pow • The parameters are a and the result of b*2 • The two are separated by commas • It is clear that the parentheses is not just for precedence, since there is no operator between the function name and parameter list Copyright © 2006 -2015 by Curt Hill
Operator Overload • When we overload an operator, we can redefine the operator – Hence the << with an ostream on the left and an int on the right is actually a function call that looks like normal operator usage – A << with an integer on either side is bit shift operator • j<<2 is similar to a multiply by 4 • Operator overloads are delayed until discussion on class definitions Copyright © 2006 -2015 by Curt Hill
Functions have a number of characteristics: • They all possess a group of statements • They are started by an invocation or call • When they are done they return to the point of the call • They may receive inputs known as parameters or arguments • They may return values, which can be retained or ignored Copyright © 2006 -2015 by Curt Hill
Now what? • Since we have used both predefined functions (such as cos) and methods (such as wx. String’s To. Int) we next need to consider how to define a function • What is the general form of a function? Copyright © 2006 -2015 by Curt Hill
General Form • A function consists of four pieces: – Type of the return value – Function name – Parameter list – Compound statement • These occur in the above order • There may be a variety of modifiers as well • Lets re-examine a main function Copyright © 2006 -2015 by Curt Hill
Remember me? #include <iostream. h> /* Our first program */ int main(int argc, char * argv[]){ cout << “Hello World"; return 0; // All done } Copyright © 2006 -2015 by Curt Hill
Notes • First item is the type of the return value – Main functions usually return an integer • Second is the function name – Each console program must have a function named main – All others are optional as needed • Third is parameter list – This one had two parameters, which we ignored • Fourth is compound statement Copyright © 2006 -2015 by Curt Hill
Return value • The first item is the type of the returned value • This may be any named type, regardless of whether a simple type or class: – double – wx. String (or any class) – void • Used when function has no value to return • This is called a procedure in many languages Copyright © 2006 -2015 by Curt Hill
Return value again • The value returned is set by the return statement • The type of the expression following return must be compatible with the function type • A void function does not need a return – Encountering the closing brace returns • A void function may also just use: return; • void functions often have a side effect Copyright © 2006 -2015 by Curt Hill
Using the returned value • The function result may be used in any kind of statement – Usually in an assignment: s = 2+sqrt(y)/3; – Anywhere: for(i=0; i<sqrt(num); i++)… • The call of a function may ignore the type – The get method of ifstreams returns a value which is often ignored – The side effect is changing the parameter Copyright © 2006 -2015 by Curt Hill
Function name • The name of the function is used to call it • It may be any C++ acceptable name • Up to this point the function header has been generated for us, so we did not care about the name that much • Now we will have to make up names that are concise and descriptive Copyright © 2006 -2015 by Curt Hill
Parameters • A function may have zero or more parameters • No parameters would be just an empty pair of parenthesis – To. Int method of wx. String • Parameters look like a declaration of a variable: type name • There may be other things that we will see later Copyright © 2006 -2015 by Curt Hill
Function descriptions • Another way to describe a function is as: header body • The header is the: – Result type – Name – Parameter list • The body is the compound statement Copyright © 2006 -2015 by Curt Hill
Prototype • A prototype is a function declaration with just a semicolon for the body • Thus the prototype for pow would be: double pow(double base, double exp); • Often showing the prototype is all the documentation we see • Header files (file of type. h) usually contain prototypes among other things Copyright © 2006 -2015 by Curt Hill
Compound Statement • The body of the function is just a compound statement • It defines what the function does and how it does it • It may declare local variables • It may use any kind of statements • Functions that return a value should have at least one return statement Copyright © 2006 -2015 by Curt Hill
Methods • Methods are just functions that belong to a class • This makes the syntax only slightly different • Consider the typical event handler for a button to close a windows application: Copyright © 2006 -2015 by Curt Hill
A Method • The method to end a windows program: • void My. Form: : Wx. Button. Click (wx. Command. Event& event) { Destroy (); } Copyright © 2006 -2015 by Curt Hill
Notes • Event handler • This is a void function – No return is needed • The method name is Wx. Button. Click • The parameter type is different but supplied • The parameter name is event • Today we neither know nor care what type wx. Command. Event is Copyright © 2006 -2015 by Curt Hill
Scope Resolution Operator • The : : operator • My. Form is the name of the class to which the Closer method belongs • Leaving this out would make Wx. Button. Click a stand alone function • This topic will be discussed more carefully when dealing with class definitions Copyright © 2006 -2015 by Curt Hill
Definition and usage • A function must be declared before it is used – Like everything else in C/C++/Java • It will be defined by the header with a compound statement • It may be declared by the prototype – The prototype shows just enough to call the function – Does not say how to implement Copyright © 2006 -2015 by Curt Hill
C program file structure • A C library usually has two source files – X. H – X. C or X. CPP • The. H file contains prototypes, constants and such things • The. C/. CPP file contains the implementations of the functions • The. C/. CPP file will be compiled into object language – The machine language Copyright © 2006 -2015 by Curt Hill
Distributions • In order to use a library two things are needed: – The header file – The object language • Thus a developer may sell a library named graph • The user receives: – graph. h – graph. obj or graph. lib – Does not need nor receive graph. cpp Copyright © 2006 -2015 by Curt Hill
Example function definition int fact(int n){ // factorial int res = 1; for(int i = 2; i<=n; i++) res *= i; return res; } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
With a prototype int fact(int n); //prototype int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } int fact(int n){ // factorial int res = 1; for(int i = 2; i<=n; i++) res *= i; return res; } Copyright © 2006 -2015 by Curt Hill
Placement in Windows Programs • Where do these functions go? • In Dev: – Prior to their use in the. cpp file – Usually after the includes – Not in the area that Dev warns against interference within • No scope resolution operator is needed Copyright © 2006 -2015 by Curt Hill
How does this execute? • Execution in console programs always starts with the main function • When a function call is encountered the following happens: • The current function is suspended • The parameters are mapped • The function is executed • The return statement is executed • The original function receives the result and continues Copyright © 2006 -2015 by Curt Hill
Tracing • Tracing into a function is interesting • The following issues need to be understood: – Scope – Parameter passage • These are handled in more detail later • For now lets just trace through this simple example: Copyright © 2006 -2015 by Curt Hill
Trace (0) int fact(int n){ // factorial int res = 1; Start in main function for(int i = 2; i<=n; i++) res *= i; k return res; } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (1) int fact(int n){ // factorial int res = 1; Read in k, 4 for(int i = 2; i<=n; i++) res *= i; k 4 return res; } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (2) int fact(int n){ // factorial int res = 1; Call function with k for(int i = 2; i<=n; i++) res *= i; k 4 return res; } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (3) int fact(int n){ // factorial int res = 1; Start function, create n for(int i = 2; i<=n; i++) res *= i; return res; k n 4 4 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (4) int fact(int n){ // factorial int res = 1; Create res for(int i = 2; i<=n; i++) res *= i; return res; k n res 4 4 1 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (5) int fact(int n){ // factorial int res = 1; Create i for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (6) int fact(int n){ // factorial int res = 1; Test is true for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (7) int fact(int n){ // factorial int res = 1; Multiply for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (8) int fact(int n){ // factorial int res = 1; Increment for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (9) int fact(int n){ // factorial int res = 1; Test is true for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (10) int fact(int n){ // factorial int res = 1; Multiply for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 } 6 int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (11) int fact(int n){ // factorial int res = 1; Increment for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 6 4 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (12) int fact(int n){ // factorial int res = 1; Test is true for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 6 4 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (13) int fact(int n){ // factorial int res = 1; Multiply for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 6 4 } int main(){ int k; 24 cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (14) int fact(int n){ // factorial int res = 1; Increment for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 6 4 24 5 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (15) int fact(int n){ // factorial int res = 1; Test is false for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 6 4 24 5 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (16) int fact(int n){ // factorial int res = 1; Return 24 for(int i = 2; i<=n; i++) res *= i; return res; k n res i 4 4 1 2 2 3 6 4 24 5 } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Trace (17) int fact(int n){ // factorial int res = 1; 24 is displayed for(int i = 2; i<=n; i++) res *= i; k 4 return res; All locals of fact are destroyed } int main(){ int k; cin >> k; cout << “Factorial is “ << fact(k); return 0; } Copyright © 2006 -2015 by Curt Hill
Notes • Function invocation involves – Suspending the caller – Creating and initializing parameters – Creating local variables, possibly with initialization – Starting the function • The return: – Restarts the caller with the new value – Destroys local variables and parameters Copyright © 2006 -2015 by Curt Hill
Scope • Notice that fact could not reference k because it is not in its scope • All it receives is a copy of k, which is the parameter • Had there been global variables both fact and main could have used them Copyright © 2006 -2015 by Curt Hill
Finally • This has been a long presentation, but there is much more to know • This includes: – Parameter passage mechanisms – More on tracing – Miscellaneous topics on functions • Functions is the last major topic we need to cover this semester Copyright © 2006 -2015 by Curt Hill
- Slides: 55