cs31 lecture 17

For the function arguments int main int tmpa 2 int int

Info iconThis preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: ers, roughly x Each character can be read one by one by indexing the Each vector vector string s = “asdfasdfasdf”; for (int i = 0; i < s.size()
; ++i)
 std::cout << s[i] << std::endl; 82 Good programming style 1. Code must be documented / commented 2. Functions should be used whenever possible Rough guideline: a function should be less than 100 lines, Rough ideally more around 25 lines ideally Namespaces should be used for multiple-file projects 1. Ok approach: 1 namespace for the full project 1. Replacing a variable definition in a sub-region should be Replacing avoided avoided 2. Global variables should be used with parsimony Global 83 Functions x Functions take 0 or more arguments, and have a return Functions type type x When called, a connection is made between the call and When the function implementation the int foo(int a, int b)
 { int c = a + b; int return c; return } int main()
 { return foo(2, 3)
; return } 84 Understanding function calls x A good first step: replace the function calls in expressions by singleassignment expressions int main() { int tmp1 = foo(2, 3)
; return tmp1; return } x A good complementary step: do the same for the function arguments int main()
 { int tmp_a = 2; int int tmp_b = 3; int int tmp1 = foo(tmp_a, tmp_b)
; return tmp1; return } 85 Pass-by-value x The “intuitive” way of passing arguments: a memory slot The is allocated for the arguments, and the value of the argum...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online