TBB1073_SPDB05 - TBB1073 SPDB Functions Objectives To be able to implement functions To become familiar with the concept of parameter passing To

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

View Full Document Right Arrow Icon
TBB1073 - SPDB 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 Objectives To be able to implement functions To become familiar with the concept of parameter passing To develop strategies for decomposing complex tasks into simpler ones To be able to determine the scope of a variable To recognize when to use value and reference parameters
Background image of page 2
3 Introduction to Function Divide and conquer Construct a program from smaller pieces or components Each piece more manageable than the original program
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 More about Function Programs written by combining new functions with “prepackaged” functions in the C++ standard library. The standard library provides a rich collection of functions . Functions are invoked by a function call A function call specifies the function name and provides information (as arguments) that the called function needs Boss to worker analogy: A boss (the calling function or caller) asks a worker (the called function) to perform a task and return (i.e. report back) the results when the task is done.
Background image of page 4
5 Calling a Function A programmer calls a function to have its instructions executed. the caller (getting things done) the function (has the modify temperature instructions)
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Types of Function Predefined Function: The use of C++ standard library function pow(2,3) sqrt(4.0) User-defined Function: Programmers create function to organize/divide tasks in a program
Background image of page 6
7 Calling a Function Execution flow during a function call
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Parameters When another function calls the pow function, it provides “inputs”, such as the values 2 and 3 in the call pow(2, 3) . In order to avoid confusion with inputs that are provided by a human user ( cin >> ), these values are called parameter values . int main() { double z = pow(2, 3); ... }
Background image of page 8
9 Implementing Functions When writing this function, you need to: Pick a good, descriptive name for the function Give a type and a name for each parameter. There will be one parameter for each piece of information the function needs to do its job. Specify the type of the return value double cube_volume(double side_length)
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Implementing Functions
Background image of page 10
11 User-Defined Functions 1. Value-returning functions : have a return type Return a value of a specific data type using the return statement 2. Void functions : do not have a return type Do not use a return statement to return a value
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 Statement Once a value-returning function computes the value, the function returns this value via the return statement It passes this value outside the function via the return statement
Background image of page 12
13 Syntax: return Statement The return statement has the following syntax: In C++, return is a reserved word When a return statement executes
Background image of page 13

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

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

This note was uploaded on 03/20/2010 for the course IMSE tb1073 taught by Professor Musharaf during the Spring '10 term at University of Malaya.

Page1 / 44

TBB1073_SPDB05 - TBB1073 SPDB Functions Objectives To be able to implement functions To become familiar with the concept of parameter passing To

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

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