Function Prototypes

Function Prototypes - f(s char s return 0 g f(23 In Classic...

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

View Full Document Right Arrow Icon
Function Prototypes People often ask about how to get started with C++ or move a project or development team to the language. There are many answers to this question. One of the simplest and best is to begin using C++ as a "better C". This term doesn't have a precise meaning but can be illustrated via a series of examples. We will cover some of these examples in forthcoming issues of the newsletter. One simple but important area of difference between C and C++ deals with the area of function definition and invocation. In older versions of C ("Classic C"), functions would be defined in this way: f(s) char* s; { return 0; } The return type of this function is implicitly "int", and the function has no prototype. In ANSI C and in C++, a similar definition would be: int f(char* s) { return 0; } Why does this matter? Well, suppose that you call the function with this invocation:
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: f(s) char* s; { return 0; } g() { f(23); } In Classic C, this would be a serious programming error, because a value of integer type (23) is being passed to a function expecting a character pointer. However, the error would not be flagged by the compiler, and the result would be a runtime failure such as a crash. By contrast, in ANSI C and in C++ the compiler would flag such usage. Very occasionally, you want to cheat, and actually pass a value like 23 as a character pointer. To do this, you can say: f((char*)23); Such usage is typically only seen in very low level systems programming. Using function prototypes in C++ is a big step forward from Classic C; this approach will eliminate a large class of errors in which the wrong number or types of arguments are passed to a function....
View Full Document

This note was uploaded on 02/27/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

Page1 / 2

Function Prototypes - f(s char s return 0 g f(23 In Classic...

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

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