Lecture6 - ECE 3090 Software for Engineering Systems Prof....

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

View Full Document Right Arrow Icon
ECE 3090 Software for Engineering Systems Prof. Hongwei Wu School of Electrical and Computer Engineering Georgia Institute of Technology (Savannah)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Today’s topic: Functions ± Defining and calling functions ± Sending data into a function ± The return statement ± Returning values from a function ± Local and global variables ± Using reference variables as parameters ± Overloading functions ± The exit() function;
Background image of page 2
A principle in software engineering: modular programming ± Modular programming is to break a program down into smaller, manageable functions or modules. ± Function is a collection of statements to perform a task ± Motivation for modular programming: ² Improves maintainability of programs ² Simplifies programming
Background image of page 3

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

View Full DocumentRight Arrow Icon
Defining and calling functions ± Function definition includes the following parts ² return type : data type of the value that the function returns to the part of the program that calls it ² name : name of the function. Function names follow same rules as variables ² parameter list : variables containing values passed to the function ² body : statements that perform the function’s task, enclosed in {}
Background image of page 4
Functional return type ± If a function returns a value, the type of the value must be indicated: int main() ± If a function does not return a value, its return type is void: void printHeading() { cout << "Monthly Sales\n"; }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Calling function ± To call a function, use the name of the function followed by a pair of parentheses (enclosing the parameter values) and a semicolon. ± The main function can call any number of functions ± Functions can call other functions void displayMessage() { cout << "Hello from the function of displayMessage.\n"; } int main () { cout << "Hello from main.\n"; displayMessage(); cout << "Back in function main again.\n"; return 0; }
Background image of page 6
± When a function is called, the program branches to that function and executes the statements in the body. ± After the function terminates, execution resumes in the calling function at point of call. ± A function must be first defined or declared before it can be called (function prototypes). void displayMessage() { cout << "Hello from the function of displayMessage.\n"; } int main () { cout << "Hello from main.\n"; displayMessage(); cout << "Back in function main again.\n"; return 0; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Function prototypes (Function Declaration) ± Compiler must know the name , return type , number of parameters , and data types of parameters of a function before that function is first called: ± Two ways to notify the compiler about a function ² Place function definition before any call to that function ² Use a function prototype (function declaration) ± A function prototype is like the function definition without the body void printHeading(); ± Function prototypes are usually placed near the top of a program, so that the compiler will encounter them before any function calls.
Background image of page 8
1. #include <iostream> 2. using namespace std; 3. 4.
Background image of page 9

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

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

Page1 / 40

Lecture6 - ECE 3090 Software for Engineering Systems Prof....

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

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