passvalue - Functions:Passing Parameters by Value...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Functions:Passing Parameters by Value Programming 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. 02 Prog Fundamentals I: Pass by Value /Slide 3 Pass by Value The arguments of a function retain their original values after the functions execution. 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? 02 Prog Fundamentals I: Pass by Value /Slide 5 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 function will be lost when the function terminates. 02 Prog Fundamentals I: Pass by Value /Slide 6...
View Full Document

This note was uploaded on 09/30/2010 for the course COMP COMP102 taught by Professor Lrr during the Fall '09 term at HKUST.

Page1 / 18

passvalue - Functions:Passing Parameters by Value...

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