lec04-functions

lec04-functions - CSC 1111 Introduction to Computing using...

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Basics of 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 Questions to be answered … What is a function? What's happening when you call a function? Transfer of control Parameters passing Returning a value from a function How to call/invoke pre-defined functions? How to define functions?
Background image of page 2
3 Outline Functions and their characteristics Calling pre-defined functions Function definition and local variables Parameters The return keyword Function prototypes (or declarations)
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 What's a function? Block of codes with a name Also known as procedure or subroutine Calling a function by its name Execute codes in that function If function A calls or invokes function B, we say A is the caller and B is the callee . Can a function be a caller and a callee at the same time?
Background image of page 4
5 #include <iostream> using namespace std; int cube( int x) { int y = x * x * x; return y; } int main() { cout << "Cube of 5 is: " << cube( 5 ) << endl; cout << "Cube of 6 is: " << cube( 6 ) << endl; return 0 ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Cube of 5 is: 125 Cube of 6 is: 216 Line 4-7: Defining a function named cube that calculates and return the cube of an integer.
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 int cube( int x) { int y = x * x * x; return y; } Characteristics of Functions Name – An identifier for programmers to refer to a block of codes Function Body – Block of codes, enclosed by ' { ' and ' } ', that makes up the function. Similar to a math function f ( x ) = x 3 but a C++ function can perform more than just calculation.
Background image of page 6
7 int cube( int x) { int y = x * x * x; return y; } Characteristics of Functions Parameters – For the caller to pass data to the callee. With parameters, we can reuse the same block of codes on different data. A function can have zero or more parameters. In math, if we define f ( x ) = x 3 , then f (0) evaluates to 0 f (10) evaluates to 1000 cout << "Cube of 5 is: " << cube( 5 ) << endl;
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 int cube( int x) { int y = x * x * x; return y; } Characteristics of Functions Return value – For callee to return a value to the caller (Communication is both way) Parameters: Caller callee Return value: Callee caller Return type – Data type of the value to be returned When this function call completes, it will be replaced by its returned value, which is 125. cout << "Cube of 5 is: " << cube( 5 ) << endl;
Background image of page 8
9 #include <iostream> using namespace std; int cube( int x) { int y = x * x * x; return y; } int main() { cout << "Cube of 5 is: " << cube( 5 ) << endl; cout << "Cube of 6 is: " << cube( 6 ) << endl; return 0 ; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 For every function call, the control flow is transferred from the caller to the callee. When the callee completes/returns, the control flow resumes in the caller. Transfer of control flow
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 Outline Functions and their characteristics Calling pre-defined functions Function definition and local variables Parameters The return keyword Function prototypes (or declarations)
Background image of page 10
11 Calling Pre-defined Functions To call a function, you have to know the following info about the function (usually from manuals) name, functionality, parameters, return value You also have to know which header file
Background image of page 11

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

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

This note was uploaded on 12/28/2010 for the course CSC CSC1110 taught by Professor Cjyuan during the Fall '06 term at CUHK.

Page1 / 42

lec04-functions - CSC 1111 Introduction to Computing using...

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

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