{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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?
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)
Image of page 3

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

View Full Document Right 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?
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.
Image of page 5

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

View Full Document Right 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.
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;
Image of page 7

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

View Full Document Right 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;
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
Image of page 9

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

View Full Document Right 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)
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
Image of page 11

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern