Lec10 - Lecture 10: Functions PIC 10A Todd Wittman Sec 5.1:...

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

View Full Document Right Arrow Icon
1 Lecture 10: Functions PIC 10A Todd Wittman Sec 5.1 : Functions s C++ is modular , which means the program can be built out of smaller components. Much like Swedish furniture. s A function is a set of instructions separate from the main routine that can be called on at any time. s Generally we pass the function parameters . s The function often returns a value. s We’ve already used several built-in functions main ( ) Line.move (dx, dy) sqrt (x) cwin.coord (x1, y1, x2, y2) getline(cin,s) setw (columnWidth) s Today we’re going to learn how to write our own functions.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Sec 5.2 : Writing Functions s When we declare a function, we need to identify the data types of the parameters that are passed to the function and the type of variable that is returned by the function. type function_name ( type param1, type param2, . .. ) { **FUNCTION CODE** } A dumb example. .. int add2Numbers ( int num1, int num2 ) { int sum = num1 + num2; return sum; } return type Parameters Braces enclose the code of the function. return ends the function, but not the whole program. Sends back a value. Calling Functions s When we call the function, we shouldn’t write out the data type. s
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/19/2011 for the course PIC 10A taught by Professor Wittman during the Fall '08 term at UCLA.

Page1 / 7

Lec10 - Lecture 10: Functions PIC 10A Todd Wittman Sec 5.1:...

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

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