Get 2 days of unlimited access
Study Guides (350,000)
US (190,000)
U of M (5,000)
ENGR (20)
Study Guide

# [ENGR 101] - Midterm Exam Guide - Comprehensive Notes for the exam (11 pages long!)

Department
Engineering
Course Code
ENGR 101
Professor
James Juett
Study Guide
Midterm

This preview shows pages 1-3. to view the full 11 pages of the document.
U of M
ENGR 101
MIDTERM EXAM
STUDY GUIDE

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Lecture 17 Functions and Scope
Function is an abstraction over a chunk of computation
o Requires explicit return statement
Interface describes how we use it
Implementation contains code to make the function work
o Can contain any kind of code
Examples
o Function to square a number
int square(int n)
return n * n;
o Function to find absolute value
Int abs(int n) {
Int a;
If(n >= 0) {
A = n;
}
else{
a = -n;
}
return a;
o Iproig As futio ultiple ays, do’t eed else)
Int abs(int n) {
If(n >= 0) {
Return n;
}
else{
return -n;
}
}
Calling functions is similar to MATLAB
o square(x)
o abs(x)
When a function is called
o The values of the arguments are copied in to the parameter variables
o The ode for the futio’s ipleetatio is ru
o As soon as a return statement is encountered, the function ends immediately
o The returned value is transferred back to the calling code where it is used
wherever the function call had appeared
C++ vs. MATLAB
o Cannot return more than one variable in C++
o Common to define several functions in the same file
o In C++, a return statement determines the return value
find more resources at oneclass.com
find more resources at oneclass.com
###### You're Reading a Preview

Unlock to view full version

Only pages 1-3 are available for preview. Some parts have been intentionally blurred.

Arguments and Parameters
o Functions may have many parameters, each with its own type
Int func(int x, string message, int y){
Int a = 3;
Int b = 4;
It  = fua, hello, ;
o First line is called Function Signature
o The ordering of arguments you pass in is used to determine what goes to which
parameter
Motivation for Functions
o Make code easier to write/understand
Making an isPrime Function
o Interface
Bool isPrime(int n);
o Implementation
Bool isPrime(int n);
For (int x=2; x>n; ++x) {
If (n%x == 0) {
Return fales;
}
}
return true;
}
void Functions
o indicates no return value
o just to hae soe side effet
o Changing pass-by-reference parameters (see below)
Exercise: Printing Triangles
o Void print_triangle_X(3) {
o For (int r = 1; r,<=3; ++r) {
o Print_row_of_X(r);
o }
o }
Scope
o A variable can only be used
After its declaration
Within its scope
o If you try to use a ariale efore its delaratio or outside its sope, you’ll get a
compiler error
Global Scope
o Variables declared outwide a function
o Can be used anywhere in the program after their declaration
o In most cases EVIL
find more resources at oneclass.com
find more resources at oneclass.com
###### You're Reading a Preview

Unlock to view full version