CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email:...
Transcript of CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email:...
![Page 2: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/2.jpg)
ontariotechu.ca2
Outline
• Functions:
– Pre-defined functions
– Programmer functions
• Overloading
• Parameters:
– Call by reference
– Call by value
![Page 3: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/3.jpg)
Functions
• Pre-defined functions are functions that are not created by the programmer but are instead created by other programmers and available as libraries
– Example libraries include the C library, the C++ libraries (including the standard template library(STL)), the Boost C++ libraries and more!
See predefined_functions_ex.cpp for examples of
using functions defined in cmath.h, ctime.h and
cstdlib.h and programmer_function_ex.cpp
3
![Page 4: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/4.jpg)
Overloading
• Same function name
• Different parameter lists
• Two separate function definitions
• Function "signature"
– Function name & parameter list must be "unique" for each function
definition
• Allows same task performed on different data
4
![Page 5: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/5.jpg)
Overloading Example: Average• Function computes average of 2 numbers:
double average(double n1, double n2)
{
return ((n1 + n2) / 2.0);
}
• Now compute average of 3 numbers:
double average(double n1, double n2, double n3)
{
return ((n1 + n2 + n3) / 3.0);
}
• Same name, two functions
5
![Page 6: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/6.jpg)
Overloaded Average() Cont’d
Which function gets called?
• Depends on function call itself:– avg = average(5.2, 6.7);
• Calls "two-parameter average()"
– avg = average(6.5, 8.5, 4.2);• Calls "three-parameter average()"
• Compiler resolves invocation based on signature of function call– "Matches" call with appropriate function
– Each considered separate function
6
![Page 7: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/7.jpg)
Parameters
Two methods of passing arguments as parameters
• Call-by-value
– "copy" of value is passed
• Call-by-reference
– "address of" actual argument is passed
7
![Page 8: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/8.jpg)
Call-by-Value Parameters
• Copy of actual argument passed
• Considered "local variable" inside function
• If modified, only "local copy" changes
– Function has no access to "actual argument“ from caller
• This is the default method
– Used in all examples thus far
8
![Page 9: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/9.jpg)
Call-By-Reference Parameters
• Used to provide access to caller’s actual argument
• Caller’s data can be modified by called function!
• Typically used for input function
– To retrieve data for caller
– Data is then "given" to caller
• Specified by ampersand, &, after type in formal parameter list
9
![Page 10: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/10.jpg)
Call-By-Reference Example
10
![Page 11: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/11.jpg)
Call-By-Reference Example
11
![Page 12: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/12.jpg)
Call-By-Reference Details
• What’s really passed in?
• A "reference" back to caller’s actual argument!
– Refers to memory location of actual argument
– Called "address", which is a unique number referring to distinct
place in memory
12
![Page 13: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/13.jpg)
Constant Reference Parameters
• Reference arguments inherently "dangerous"– Caller’s data can be changed
– Often this is desired, sometimes not
• To "protect" data, & still pass by reference:– Use const keyword
• void sendConstRef(const int &par1, const int &par2);
• Makes arguments "read-only" by function
• No changes allowed inside function body
13
![Page 14: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/14.jpg)
14
See programmer_function_ex1b.cpp,
programmer_function_ex2.cpp,
programmer_function_ex2b.cpp
![Page 15: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/15.jpg)
Please let me know about your thoughts: the course,
the instructor (me), TAs, etc.
http://freesuggestionbox.com/pub/inqcjsz
15
Feedback
![Page 16: CSCI 1060U Programming Workshop I · Lecture 7: Functions Rohollah Moosavi Email: rohollah.moosavi@uoit.ca. ontariotechu.ca 2 Outline • Functions: –Pre-defined functions –Programmer](https://reader033.fdocuments.net/reader033/viewer/2022050408/5f8530eafe3d78777e203171/html5/thumbnails/16.jpg)
ontariotechu.ca