lecture1b - Lecture1:C+ReviewPartii. ECE2574...

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

View Full Document Right Arrow Icon
1 Lecture 1 : C++ Review Part ii. ECE 2574 Chao Huang Virginia Tech Spring 2010
Background image of page 1

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

View Full DocumentRight Arrow Icon
 C. Huang, Virginia Tech 2 C/C++ functions A C/C++ function is a set of instructions that are isolated from  the rest of the program (sometimes called "subroutine"  (Fortran) or "procedure" (Pascal, PL/I))  Why use functions? » readability » reusability The ideal function does only one thing   and does it very well
Background image of page 2
 C. Huang, Virginia Tech 3 Simple example #include <iostream> using namespace std; void times_2(int num); void main(void) { int i = 57; times_2(i); cout<<"i="<<i<<endl; } void times_2(int num) { num = num * 2; } Function prototype:  it describes the function  (it is needed in this case  for the compiler's sake) Actual parameter list Formal parameter list Function statements
Background image of page 3

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

View Full DocumentRight Arrow Icon
 C. Huang, Virginia Tech 4 Call by Value C uses a convention known as  call by value  to send  parameters from a calling function to a called function The parameters in the function call are referred to as “actual  parameters” Copies of the actual parameters are created within the scope of  the called function;  these are called “formal parameters” The function can do whatever it likes with the formal  parameters, and that will never change the actual parameters. If the name of an array is used as an actual parameter, a  pointer is passed automatically
Background image of page 4
 C. Huang, Virginia Tech 5 Another Example: Passing a Pointer #include <iostream> using namespace std; void times_2(int *num); void main(void) { int i = 57; times_2(&i); cout<<"i="<<i<<endl; } void times_2(int *num) { *num = *num * 2; return; } Understanding this  Example is very  very very important
Background image of page 5

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

View Full DocumentRight Arrow Icon
 C. Huang, Virginia Tech 6 C++ also supports  call by reference
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/28/2010 for the course ECE 2574 taught by Professor Chuang during the Spring '08 term at Virginia Tech.

Page1 / 22

lecture1b - Lecture1:C+ReviewPartii. ECE2574...

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

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