slide1
Download
Skip this Video
Download Presentation
CS 105 Lecture 10 Functions

Loading in 2 Seconds...

play fullscreen
1 / 21

CS 105 Lecture 10 Functions - PowerPoint PPT Presentation


  • 115 Views
  • Uploaded on

CS 105 Lecture 10 Functions. Version of Mon, Mar 28, 2011, 3:13 pm. Quiz 2 Scores. Hmm. 17 Scores: 474338 3734 32 3229 2722 22 21 2114109 9 Avg 26.3/50 (or 52.6/100)Std dev 11.7. Quiz 2 Scores. Numerous problems came from Labs. Functions.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CS 105 Lecture 10 Functions' - mara-peters


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

CS 105 Lecture 10

Functions

Version of Mon, Mar 28, 2011, 3:13 pm

quiz 2 scores
Quiz 2 Scores
  • Hmm....

17 Scores:

474338 3734 32 3229 2722 22 21 2114109 9

Avg 26.3/50 (or 52.6/100)Std dev 11.7

quiz 2 scores1
Quiz 2 Scores
  • Numerous problems came from Labs
functions
Functions
  • Function: A discrete piece of code that performs a specific operation or task
  • Named with a descriptive identifier
  • Called from main() or another function
  • When called, program control (execution) is transferred to the function.
  • Function performs required tasks, and then possibly returns a value.
  • After return from function, control returns to the statement following the function call.
function attributes
Function Attributes
  • Function Name: Identifier used to call function.
  • Function Parameter(s) or Argument(s): value(s) passed into function for use by function code.
  • Function Return Value: Value returned by function back to calling function.
math library
Math Library
  • Provides a library of common math functions. Get via #include<cmath>
  • Must call functions with correct number and order of parameters.
  • Functions return value after calculations.
  • Example: The power function pow; square root function sqrt.
  • double bigValue;
  • double base = 2.0, exponent = 20.0;
  • bigValue = pow(base, exponent);
  • cout << bigValue << " " << sqrt(bigValue) << endl;
user defined functions
User-Defined Functions
  • We can write our own functions; we need to specify the name of the function, the number and kinds of parameters it takes, and the kind of value it returns (if any).
    • Plus, the "body" of the function — the code that does the calculation
  • Example:
  • double calcAverage(int total, int numItems)
  • {
  • double result = (double) total / numItems;
  • return result;
  • }
define function before main
Define Function Before Main
  • #include <iostream>
  • using namespace std;
  • double calcAverage(int total, int numItems)
  • {
  • double result = (double) total / numItems;
  • return result;
  • }
  • int main()
  • {
  • int sumGrades = 921, numStudents = 10;
  • double avgGrade;
  • avgGrade = calcAverage(sumGrades,numStudents);
  • cout << avgGrade << endl;
  • cout << calcAverage(1200, 14) << endl;
  • }
define functions after main
Define Functions After Main?
  • If you have many functions, then you might have a lot of code to get through before you actually get to the main program.
  • Nice to have the main program first and the functions afterward.
    • But what if we use calcAvg before we give its definition?
    • How will the compiler know if we\'re calling calcAvg correctly?
use function prototype
Use Function Prototype
  • A function prototype tells the compiler how we\'ll use the function. (Omits the function body.)
  • #include <iostream>
  • using namespace std;
  • Function Prototype "declares" calcAverage
  • double calcAverage(int total, int numItems); ←–––––– NOTE !
  • int main()
  • {
  • // etc
  • }
  • "Definition" of calcAverage gives its body
  • double calcAverage(int total, int numItems)
  • {
  • // rest of body as before
  • }
declaring a function
Declaring a Function
  • Function Prototype: Declares a function\'s name and how it is called; it doesn\'t define the body of the function.
  • Used by the compiler check for syntax errors in how we call the function
  • Example:
  • double calcAverage(int total, int numItems);
    • double is the return type.
    • calcAverage is the function name.
    • total is the first parameter and it has type int
    • numItems is the second parameter and it has type int.
  • Standard functions (e.g., those in the STL = Standard Template Library) have function prototypes in header files that we #include.
functions return types
Functions – Return Types
  • A function with a return type of int must have a return of some integer.
    • Similarly, a function with a return type of double must have a return of some double, etc.
    • That\'s why main programs include return0;
  • A function with void as its return type doesn\'t return a value. (A.k.a. "void" function)
    • It can have a return; statement (with no value).
    • Or it can fall off the end of the function (a return; is assumed).
parameters arguments
Parameters & Arguments
  • May pass as many parameters as necessary to function.
  • A copy of the value of the parameter is passed to the function.
  • Changing the value of the parameter in the function does not affect the value of the original variable.
  • This is called Pass-by-Value
example of void function
Example of void function
  • #include <iostream>
  • using namespace std;
  • void printNum(int); // function prototype
  • int main()
  • {
  • int myNumber = 7;
  • printNum(myNumber); // function call
  • printNum(9);
  • return 0;
  • }
  • // begin function definition
  • void printNum(int numToPrint)
  • {
  • cout << numToPrint;
  • }
functions1
Functions
  • Write a function that accepts one integer as a parameter. The function returns the sum of the integers from 1 to the integer passed to the function.
  • 5 minutes …… GO!
sum function
Sum Function
  • int sumOfInt(int num)
  • {
  • int i, sum = 0;
  • for(i = 1; i <= num; i++)
  • sum = sum + i;
  • return(sum);
  • }
function calls
Function Calls
  • Write a function call to call sumOfInt with an argument of 5, save the return value into a variable and print it out.
  • 2 minutes – GO!
function calls1
Function Calls
  • int sumOfInt(int);
  • int main()
  • {
  • int sum5;
  • sum5 = sumOfInt(5); cout << sum5 << endl;
  • return 0;
  • }
display line function
Display Line Function
  • Write a function that displays a character some number of times to the display. The function is passed the character and the number of times to display it.
  • 5 minutes: GO!
displaychar function
displayChar() Function
  • void displayChar(char charPassed, int times)
  • {
  • int i;
  • for(i = 0; i < times; i++)
  • cout << charPassed;
  • cout << endl;
  • }
slide21
do
  • {
  • theLabs(threeHoursAWeek);
  • }
  • while (!semesterDone);
ad