Download presentation
Presentation is loading. Please wait.
Published byCarol Craig Modified over 8 years ago
1
CSC 107 – Programming For Science
2
Today’s Goal Write functions that take & return values How parameters declared and how we call functions What it means when we pass a value as a parameter return statement’s meaning and how it works When and why we use the return statement Why skipping return statements are bad ideas
3
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
4
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
5
Functions All functions similar, differ only in who wrote it Built-in Function WriterUser-Defined Function Writer
6
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:
7
Function Declaration
8
Return Type Functions must specify their return type Type of value returned by the function float 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);
9
Function Definitions
10
return Statement
11
return Examples
18
Function ends when return is executed Any and all code after return will be ignored Calling function will resume its execution common There are no errors or warnings for this common bug return Statement
19
return Examples
21
Multiple return Statements Multiple return s possible in a single function Each time function is called, only one is executed Gives greater flexibility by not tying code down bool getNumber() { int num; cout > num; if ( (num % 2) == 1) { return true; } else { return false; } }
22
Variables Variable Variable names location to store data Memory location's initial value is unknown Assignments update memory location with new value Memory location updated by assignment ONLY When variable is used in program… …uses current value at that memory location
23
Variable Scope Scoping rules specify variables' lifetimes Variables not universal Variables not universal, but have specific lifetimes Variable usable only in braces in which declared For this copy of variable, scope defines its lifetime Variable "dies" with end of scope in which declared At start of scope, new copy created Cannot use outside scope: error for bad variable Must have unique names within a scope Can reuse names between scopes – meaning is clear
24
Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { int num = 3; readNumber(num); readNumber(5); return 0; }
25
Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int main() { int num = 3; readNumber(num); readNumber(5); return 0; } One name -but- two memory locations One name -but- two memory locations
26
Global Variables Global variables are evil Name for variables declared outside of any function Exists throughout the entire program Since they are global, can be used in any function Functions can make variable with name of global Within this function, would use local variable Good luck figuring code out; prayers for debugging it
27
Global Variable Scope void readNumber(int len) { int num = 0; for (int i = 0; i > ch; num *= 10; num += ch – '0'; } cout << num << endl; } int num = 3; int main() { readNumber(num); readNumber(5); return 0; } Scopes overlap -but- two memory locations Scopes overlap -but- two memory locations
28
Global Variables Evil idea sold by suits like this guy
29
Functions' Parameters
30
Parameters are Variables Just like variables, they name memory location Get new location each time function is called Value stored at location changed by assignments Unrelated to other variables even if names overlap assignments copy value Like Xerox machine, assignments copy value
31
Calling Function w/Arguments No different than we have been doing forever Need name & parentheses like all function calls Arguments specify value for each parameter Must Must have equal number of arguments & parameters Argument that is 1 st in parens is value of 1 st parameter 2 nd parameter uses 2 nd argument in function call, etc.
32
Will It Compile?
39
Parameters Get Initialized Assigned value of argument at start of function Argument could be literal, variable or expression Works like normal assignment; no connection made WILL NOT Assigning parameter WILL NOT update argument Like Xerox machine, assignments copy value
40
Sample Trace double powerLoss(double wattage) { double current = wattage / 110; wattage = current * current; return wattage; } int main() { double watt = 1100; double printed = powerLoss(watt); cout << printed << " " << watt << endl; double wattage = 0; printed = powerLoss(11.0); cout << printed << " " << wattage << endl; }
41
Your Turn Get into your groups and try this assignment
42
For Next Lecture Read about parameters in Section 9.4.4, 9.6 How do we pass references to a function? What does it mean to pass-by-reference? What is difference with what we discussed today? Weekly Assignment #7 out & due on Tues. Avoid the rush by start working on it now Project #2 available so start working on it now Project solution relies on topics up through today
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.