passvalue

passvalue - Programming Functions:Passing Parameters by...

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

View Full Document Right Arrow Icon
Functions:Passing Parameters by Value Programming
Background image of page 1

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

View Full DocumentRight Arrow Icon
02 Prog Fundamentals I: Pass by Value /Slide 2 Return Value A function normally returns a single result, if it is not a void function. At least one of the statements in the function body should have the form: return <expression>; The value passed back by the return statement should have the same type as the function.
Background image of page 2
02 Prog Fundamentals I: Pass by Value /Slide 3 Pass by Value The arguments of a function retain their original values after the function’s execution.
Background image of page 3

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

View Full DocumentRight Arrow Icon
02 Prog Fundamentals I: Pass by Value /Slide 4 Pass by Value: Example 1 For example, consider the following code: int sum( int num_1, int num_2){ num_1 = num_1 + num_2; return num_1; } int main(){ int var_x, var_y, var_z; var_x = 3; var_y = 5; var_z = sum(var_x, var_y); cout << var_z << endl; return 0; } What are the values of var_x , var_y , and var_z at the end of the main() program?
Background image of page 4
Pass by Value: Example 1 The answer: 3, 5, and 8. Even though the value of parameter num_1 is changed, the corresponding value in argument var_x does not change. The value of the argument is copied to the parameter, but changes to the value of the parameter do not affect the argument. In fact, all information in local variables declared within the
Background image of page 5

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

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

This note was uploaded on 08/07/2011 for the course COMP 102 taught by Professor Daniel during the Spring '08 term at HKUST.

Page1 / 17

passvalue - Programming Functions:Passing Parameters by...

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

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