lec04-functions

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

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

1 CSC 1111 Introduction to Computing using C++ Basics of Functions

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

View Full Document
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?
3 Outline Functions and their characteristics Calling pre-defined functions Function definition and local variables Parameters The return keyword Function prototypes (or declarations)

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

View Full Document
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?
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.

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

View Full Document
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.
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;

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

View Full Document
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;
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

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

View Full Document
10 Outline Functions and their characteristics Calling pre-defined functions Function definition and local variables Parameters The return keyword Function prototypes (or declarations)
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

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

View Full Document
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
Ask a homework question - tutors are online