Declaring Function Prototypes


A function prototype is a single-line statement that introduces the name of a function to the compiler. The prototype also indicates what types of parameters can be passed into the function and what type of value the function returns.

Declaring a Simple Function Prototype

The following example shows a simple function prototype:

void DisplayWelcome();

In this example, the name of the function is DisplayWelcome. The parentheses are required to indicate that this is a function. The parentheses are empty in this example, which means that the function doesn’t take any parameters. The void keyword at the beginning of the function prototype indicates that the function doesn’t return a value either—presumably, the function just displays a welcome message on the screen.

Note

Some programming languages differentiate between functions (which return a value) and subroutines (which do not return a value). For example, Microsoft Visual Basic .NET uses the Function keyword for functions and the Sub keyword for subroutines. C++ only has functions; use the void return type if the function doesn’t return a value. Notice the semicolon at the end of the function prototype. The semicolon is a statement terminator, and it marks the end of the function prototype. A function prototype doesn’t tell you what the function does; it just tells you the function signature.

In this exercise, you will declare a simple function prototype in a Visual C++ application. The function will not take any parameters, and it will not return a value either.

  1. Start Microsoft Visual Studio .NET, and open a new Visual C++ Console Application (.NET) project named InvestmentPlanner.

  2. From the View menu, select the menu command Solution Explorer.

  3. In Solution Explorer, double-click InvestmentPlanner.cpp to view the code in this file.

  4. At the top of the file, immediately under the using namespace System; line, add the following function prototype:

    void DisplayWelcome();

    This line is the function prototype you saw earlier. You place function prototypes near the top of the source file so that they are visible to the rest of the code in the file.

  5. From the Build menu, select the menu command Build Solution to build your program and check that there are no syntax errors. There’s no point in running the program yet because you haven’t implemented or called the DisplayWelcome function. You’ll do that later in this chapter.

Declaring Parameters in a Function Prototype

Functions can take parameters to make them more generic. You must declare the data types for these parameters in the function prototype.

In this exercise, you will declare a function prototype that uses parameters.

  1. Continue working with the project you created in the previous exercise.

  2. Add the following function prototype:

    void DisplayProjectedValue(double amount, int years, double rate);

    This function prototype declares a function named DisplayProjectedValue. The function takes three parameters: a double, an int, and another double. The compiler uses this information to ensure that the function is always called with the correct number and types of parameters.

    Tip

    Parameter names are optional in the function prototype. Strictly speaking, you could omit the parameter names and just specify the parameter types. However, parameter names help to convey the meaning of the parameters, so it’s good practice to use them.

  3. Build your program to check the syntax.

Declaring the Return Type in a Function Prototype

As well as specifying input parameters for a function, you must also specify a return type for the function. As you saw earlier, the void return type indicates that the function does not return a value.

In this exercise, you will see how to specify a non-void return type for a function.

  1. Continue working with the project from the previous exercise.

  2. Add the following function prototype:

    double GetInvestmentAmount();

    This function prototype declares a function named GetInvestmentAmount. The function doesn’t take any parameters, but it returns a double.

  3. Add another function prototype as follows:

    int GetInvestmentPeriod(int min, int max);

    This example shows how to declare a function that takes parameters and returns a value. The GetInvestmentPeriod function takes two int parameters and returns an int.

    Note

    The parameter types and return type are independent of each other. The fact that the GetInvestmentPeriod parameters and return type are all int is entirely coincidental. It’s quite easy to imagine a function whose parameter types and return type are different, as shown in this example:

    double CalculateAverageValue(int number1, int number2);
  4. Build your program.

Declaring Default Values for Function Parameters

When you declare a function prototype, you can specify default values for some or all of its parameters. Default values are useful for parameters that usually have the same value each time the function is called. Specifying a default value for a function parameter means that you can omit the parameter value when you call the function; the compiler will substitute the default value on your behalf.

In this exercise, you will define default parameters in one of the function prototypes you declared earlier.

  1. Continue working with the project from the previous exercise.

  2. Find the following function prototype:

    int GetInvestmentPeriod(int min, int max); 
  3. Modify the function prototype as follows to define default parameter values:

    int GetInvestmentPeriod(int min=10, int max=25);

    This function prototype has two parameters named min and max. The parameters are followed by an equal sign (=) and then a default value. We have defined a default value of 10 for the min parameter and a default value of 25 for the max parameter. You’ll see how to call this function in the “Calling Functions” section later in this chapter.

  4. Build your program.




Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
EAN: N/A
Year: 2003
Pages: 208

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net