Introduction to Functions

Introduction to Functions - Introduction to Functions...

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

View Full Document Right Arrow Icon
Introduction to Functions Parameter Passing, Return Values and the Difference Between Local and Global Variables.
Background image of page 1

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

View Full DocumentRight Arrow Icon
You’ve already been using functions. Recall that a program consists of one or more functions. The function main is where it all begins. To have functions we need a function definition: doit() { printf(“Hello World\n”); } And we need a function invocation” doit();
Background image of page 2
A Simple Example doit() { // a function definition printf(“Hello World!\n”); } main() { doit(); // a function invocation } NOTE: CodeWarrior by default assumes that all functions must have a prototype. This is not part of the C language! To suppress the “prototype” warning message, un-select “require function prototypes” in the C/C++ Language preferences in the “C Console App Debug Settings” from the “Edit menu”
Background image of page 3

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

View Full DocumentRight Arrow Icon
The Syntax of Functions A function definition looks as follows int myFunction(int x, int y) { printf(“as many statements as I need”); } definition of the parameters, listed in order separated by commas. Each parameter has its type specified (like a variable definition). declaration of the return type the function “body”, i.e. the statements that get executed when the function is invoked. the body is delimited by { and }
Background image of page 4
Where will it end? When a function is invoked (AKA “called”), the computer immediately begins executing statements from the beginning of the called function. Every time you call it, execution always starts at the beginning of the function. Execution continues inside the called function until either: We reach the right } at the end of the function We execute a return statement. Either way, the function stops at this point and execution picks up right where it had left off in the original function (e.g., back in main )
Background image of page 5

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

View Full DocumentRight Arrow Icon
Avoid Repeating Code, use functions! Functions are useful for avoiding writing the same code over and over. For EE312, any section of a program, 3-lines or longer, must never be repeated. Instead, take those three lines and put them in a function. void printMyName(void) {
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

Introduction to Functions - Introduction to Functions...

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

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