lecture7%2Dpost

lecture7%2Dpost - Wednesday, October 27 FunctionReview

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Wednesday, October 27 th Function Review Program Design and Decomposition Typecasting If we have some time, we’ll start  ASCII
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function Review The  foo  function computes the  average of two numbers and: 1. Returns  the sum of the numbers. 2. Sends back the average of the  numbers in the  third reference  parameter . Whats  wrong  with it? VOID foo(int n1, n2, int avg) ( int sum; sum += num1; sum += num2; return(sum);    avg = sum / 2; ] int main(void) { int mean, sum; sum = func(5,9,mean); cout << sum << “ “ << mean; } { = 0; avg = sum / 2; return(sum); } int foo(int n1, n2, int avg) int foo(int n1, int n2, int avg) int foo(int n1, int n2, int avg) VOID foo(int n1, n2, int avg) ( int sum; sum += num1; sum += num2; return(sum);    avg = sum / 2; ] int main(void) { int mean, sum; sum = func(5,9,mean); cout << sum << “ “ << mean; }
Background image of page 2
Function Review What does the  following program print  out? What is the value of  credit after lines  1 2   and  3  have executed. float credit; void charge(float amount) { if (amount <= credit) { cout << “Approved.\n”; credit -= amount; } else cout << “Rejected!\n”; } int main(void) { credit = 100.00; // starting charge(52); // line 1 charge(10); // line 2 charge(40); // line 3 }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Function + Static Review float moving_avg(float cur) // avg of last 3 { static float s1,s2,s3; s1=s2; s2=s3; s3=cur; return((s1+s2+s3)/3); } int main(void) { float avg; avg=moving_avg(3); cout << avg << “\n”; avg=moving_avg(6); cout << avg << “\n”; avg=moving_avg(1); cout << avg << “\n”; avg=moving_avg(2); cout << avg << “\n”; } What does it print? s1 0 s2 0 s3 0 avg 3 cur 3  3 (0+0+3)/3 1 6 cur 6  3  6 (0+3+6)/3 3  1  3 1 cur 1  3  6  1 (3+6+1)/3 3.3 3.3 Etc…
Background image of page 4
Reference Review { byValue = 10; byReference = 20; } void main() { int a = 5, b = 6; Silly(a,b); cout << a << “ “ << b; } When we pass a variable without using  a reference,  it is called  passing by value . When we pass a variable using a reference, it is called  passing by reference . When we pass by value,  the program makes a  copy  of the actual  parameter and sends it  to the function.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Reference Review { byValue = 10; byReference = 20; } void main() { int a = 5, b = 6; Silly(a,b); cout << a << “ “ << b; } When we pass a variable without using  a reference,  it is called  passing by value . When we pass a variable using a reference, it is called  passing by reference . When we pass by  reference , the program  references the actual  parameter in the calling  function.
Background image of page 6
void Silly(int byValue, int byReference) { byValue = 10; byReference = 20; } void main() { int a = 5, b = 6; Silly(a,b); cout << a << “ “ << b; }                  a    5                           b   
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/10/2012 for the course CS 31 taught by Professor Melkanoff during the Winter '00 term at UCLA.

Page1 / 42

lecture7%2Dpost - Wednesday, October 27 FunctionReview

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online