< C++ .NET Loop and Decision 5 | Main | C++ .NET Functions 2 >


 

 

C++ .NET Functions 1

 

 

The following are the topics available in this module.

  1. Why Use Functions?

  2. Declaring Function Prototypes

  3. Declaring a Simple Function Prototype

  4. Declaring Parameters in a Function Prototype

  5. Declaring the Return Type in a Function Prototype

  6. Declaring Default Values for Function Parameters

-----------------Next------------------------------------------

  1. Defining Function Bodies

  2. Defining a Simple Function Body

  3. Defining a Function Body That Uses Parameters

  4. Defining a Function Body That Returns a Value

  5. Calling Functions

  6. Calling Functions in the Sample Application

  7. Stepping Through the Application with the Debugger

  8. Understanding Local and Global Scope

  9. Overloading Functions

  10. Quick Reference

Why Use Functions?

 

There are many good reasons for dividing a program into functions as listed below.

  1. Each function is usually quite short and discrete. It’s easier to write a program as a series of functions than as a single long one, because you can concentrate on one function at a time.

  2. It’s also easier to read and debug a program that contains lots of small functions than one that contains a single long one, because you don’t have to remember what the whole program is doing.

  3. Functions are reusable. Once you’ve written a function, you can call it whenever you need it in your program, which reduces coding effort, and therefore improves developer productivity.

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. 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.

Start Microsoft Visual C++/Studio .NET, and create a new CLR Console Application project named InvestmentPlanner.

 

 

From the View menu, select the menu command Solution Explorer. In Solution Explorer, double-click InvestmentPlanner.cpp to view the code in this file. 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. 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 module.

 

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.

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

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. 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.

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.

Continue working with the project from the previous exercise.

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.

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. 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);

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.

Continue working with the project from the previous exercise.

Find the following function prototype:

int GetInvestmentPeriod(int min, int max);

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.

Build your program.

 

 

 

 

 

Part 1 | Part 2 | Part 3


< C++ .NET Loop and Decision 5 | Main | C++ .NET Functions 2 >