CSC 107 – Programming For Science. Today’s Goal Discuss writing functions that return values ...

21
LECTURE 17: FUNCTION RETURN CSC 107 – Programming For Science

Transcript of CSC 107 – Programming For Science. Today’s Goal Discuss writing functions that return values ...

Page 1: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

LECTURE 17:FUNCTION RETURN

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Today’s Goal

Discuss writing functions that return values return statement’s meaning and how it

works When and why we use the return

statement Why skipping return statements are bad

ideas

Page 3: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Why We Use Functions

Simplify code Replace copies of code by placing in single

location Locate commonly-used computations &

actions Good to find code, but can only return a

single value Input & output performed in these

functions Will discuss ways to change parameters’

values

Page 4: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Functions

Already been programming with functions Built-in functions like pow, exp, & log Writing & using programmer-defined

function: main All functions similar

Will be using same process to call function Handling return result same for all functions Process is same for variables, scoping,

passing data

Page 5: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Functions

All functions similar, differ only in who wrote it

Built-in Function WriterUser-Defined Function Writer

Page 6: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Function Names

Rules over legal names identical to variables Letter or underscore start name for it to be

legal Can use any letters, numbers, or

underscore after Names should be unique (including

variables) Function names rely upon relaxed style

rules Start with a letter & make name

meaningful Names should only use words you would

say to:

Page 7: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Function Declaration

List prototypes for functions near top of file For multiple files, simplifies finding what is in each

file After #includes and using statements in file If function call before it is defined in file, it is

required Return type, name, & parameters listed only

After these are listed, end with semi-colon Information compiler needs to know calls are legalfloat squareNumber(float x);double computeAvg(int x, int y);int promptAndReadInput();bool noParams(void);

Page 8: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Return Type

Functions must specify their return type Type of value returned by the functionfloat abs(float x);double pow(double x, double t);int main();

Use special return type, void, if no value is returned void goodSoldier();void doSomethingWithX(int x);

Page 9: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Function Definitions

Eventually must actually write the function Specifies code to run when function is called Not a part of main: must be outside other

functionsvoid nothingReturned(int x) { // Code goes here, skipped for space}

float notTitanic(int men, int women) { // Code goes here, skipped for space}

int main() { // Code goes here, skipped for space}

Page 10: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

return Statement

Used by function to specify value to return Value must be appropriate for function’s

return type Since not returning data, void functions

cannot use Statement written as: return value;

Value could be a literal, variable, or expression

Must be of appropriate type; no limits otherwisereturn 4;return varX;return varX * 2.1 + 45 == 0 && 4 < y;return pow(varX, 2.52);

Page 11: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

return Examples

void printText() { cout << “Nothing to see here” << endl; return 23;}

float getNumber() { double val; cout << “Enter a number: ” << endl; cin >> val; return val;}

char getEncryptedLetter() { char ch; cout << “Enter a letter: ” << endl; cin >> ch; return ch; ch += 3;}

Page 12: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

return Examples

void printText() { cout << “Nothing to see here” << endl; return 23;}

float getNumber() { double val; cout << “Enter a number: ” << endl; cin >> val; return val;}

char getEncryptedLetter() { char ch; cout << “Enter a letter: ” << endl; cin >> ch; return ch; ch += 3;}

Page 13: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

return Examples

void printText() { cout << “Nothing to see here” << endl;}

float getNumber() { double val; cout << “Enter a number: ” << endl; cin >> val; return val;}

char getEncryptedLetter() { char ch; cout << “Enter a letter: ” << endl; cin >> ch; return ch; ch += 3;}

Page 14: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

return Examples

void printText() { cout << “Nothing to see here” << endl;}

float getNumber() { double val; cout << “Enter a number: ” << endl; cin >> val; return val;}

char getEncryptedLetter() { char ch; cout << “Enter a letter: ” << endl; cin >> ch; return ch; ch += 3;}

Page 15: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

return Examples

void printText() { cout << “Nothing to see here” << endl;}

float getNumber() { float val; cout << “Enter a number: ” << endl; cin >> val; return val;}

char getEncryptedLetter() { char ch; cout << “Enter a letter: ” << endl; cin >> ch; return ch; ch += 3;}

Page 16: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

return Examples

void printText() { cout << “Nothing to see here” << endl;}

float getNumber() { float val; cout << “Enter a number: ” << endl; cin >> val; return val;}

char getEncryptedLetter() { char ch; cout << “Enter a letter: ” << endl; cin >> ch; return ch; ch += 3;}

Page 17: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Function ends when return is executed Any and all code after return will be

ignored Calling function will resume its execution There are no errors or warnings for this

common bug

return Statement

Page 18: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

return Examples

void printText() { cout << “Nothing to see here” << endl;}

float getNumber() { float val; cout << “Enter a number: ” << endl; cin >> val; return val;}

char getEncryptedLetter() { char ch; cout << “Enter a letter: ” << endl; cin >> ch; return ch + 3;}

Page 19: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Multiple return Statements

Multiple returns possible in a single function Each time function is called, only one is executed Gives greater flexibility by not tying code down

bool userTypedOddNumber() { int num; cout << “What’s your number?” << endl; cin >> num; if ( (num % 2) == 1) { return true; } else { return false; }}

Page 20: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

Your Turn

Get into your groups and try this assignment

Page 21: CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.

For Next Lecture

Read about parameters in Section 9.4 – 9.4.3 How do we pass values to a function? How does a function take in those values? What can a function do to those values?

Weekly Assignment #6 out & due next Tues. Avoid the rush by start working on it now

Programming Assignment #2 now on Angel This is a larger assignment and due in 3

weeks