Session_4 - Lesson 6 Functions A function is a subprogram...

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

View Full Document Right Arrow Icon
Lesson 6 - Functions A function is a subprogram that can act on data and return a value. Every C++ program has at least one function, main() which is called automatically when your program starts. The function main() might call other functions. Each function has its own name, and when that name is encountered, the execution of the program branches to the body of that function which is called calling the function. When the function returns, execution resumes on the next line of the calling function. Functions return a value called the return value. The type of that return value must be declared. If you write int MyFunction(); you are declaring that myFunction will return an integer value. You can also send values into the function. The description of the values you send is called a parameter list. int MyFunction( int someValue, float someFloat); The above declaration says that myFunction returns an integer but it takes an integer value and float as parameters. A parameter describes the type of the value that will be passed into the function when it is called. The actual values you pass into the function are called arguments: int x; x = myFunction(5, 3.14); Declaring and Defining Functions Using functions requires that you declare the function and define it. The declaration tells the compiler the name, return type and parameters of the function. The definition tells the compiler how the function works. A declaration of a function is called a prototype . Although you can define the function before using it and avoid the necessity of creating a function prototype, this is not a good programming practice because: It is unreasonable to insist that functions appear in a file in a particular order. Also, it is possible that function A needs to call function B and function B needs to call function A under some special circumstances. This would make it impossible to order the prototypes. Prototypes make a good debugging tool. If your prototype declares that your function takes particular set of parameters or that it returns a particular type of value, the usage of the function must match the prototype. Page 1 of 14
Background image of page 1

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

View Full Document Right Arrow Icon
Function Prototypes The function prototype is a statement, which means it ends with a semicolon. It consists of the function’s return type and signature. A function signature is its name and parameter list. In other words, everything about a function except its return type is its signature. The parameter list is a list of all the parameters and their types, separated by commas. The function prototype and the function definition must agree exactly on their return type and signature. If they do not, you will get a compile-time error. As an example, #include <iostream> using namespace std; float add_funct( int x, int y); int main() { int a = 1, b = 2; cout << "The sum of a and b is: " << add_funct(a,b) << endl; return 0; } int add_funct( int p, int q) { return p + q; } The above produces a compiler error. The following works and is accepted by most developers:
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.

{[ snackBarMessage ]}

Page1 / 14

Session_4 - Lesson 6 Functions A function is a subprogram...

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