CS31-4 - CS 31 Introduction to Computer Science I Topic 4...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-1 CS 31 : Introduction to Computer Science I Topic 4 Parameters and Overloading
Image of page 1

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-2 Parameters Function Parameters Provide data for functions Can provide a way to return values Placeholders for arguments in calls
Image of page 2
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-3 Parameters Function Parameter Types Two Different Types Call-by-value parameters Call-by-reference parameters
Image of page 3

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-4 Parameters Call-By-Value Parameters Input only: Expression (or variable) Actually local variables in the function Exist during execution of the function Initialized to value of argument on call Can be changed inside function Existence ends at function return
Image of page 4
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-5 Parameters Call-By-Value Example void v(int p1, bool p2, float p3) { p1 = 4; p2 = false; p3 = 19.8; }
Image of page 5

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-6 Parameters Call-By-Value Example int main() { void v(int p1, bool p2, float p3); int a1 = 24; bool a2 = true; float a3 = 80.4; v(a1, a2, a3); return 0; }
Image of page 6
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-7 Parameters Call-By-Value Example Arguments are copied to parameters Parameters are local variables in called Calling Function 24 true 80.4 int: a1 bool: a2 float: a3 Called Function 24 true 80.4 int: p1 bool: p2 float: p3 At Entry
Image of page 7

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-8 Parameters Call-By-Value Example Arguments are copied to parameters Parameters are local variables in called Calling Function 24 true 80.4 int: a1 bool: a2 float: a3 Called Function 4 false 19.8 int: p1 bool: p2 float: p3 At Return
Image of page 8
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-9 Parameters Call-By-Reference Parameters Can be used for input and output Must be a variable only: no expressions Use actual variables in the calling function Exist during execution of the calling function Changes in function change actual variable Existence ends at calling function return
Image of page 9

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

View Full Document Right Arrow Icon
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-10 Parameters Call-By-Reference Parameters Compiler handles the details Denoted by use of & character Attached to end of type name or beginning of parameter name in prototype and function definition header
Image of page 10
CS 31: Introduction To CS I Topic 4: Parameters & Overloading 1/2010 John A. Rohr All Rights Reserved JAR 5-11 Parameters Call-By-Reference Example void r(int &p1, bool &p2, float &p3) { p1 = 4; p2 = false; p3 = 19.8; }
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern