Functions

Functions

Copyright: © 2024 |Pages: 57
DOI: 10.4018/979-8-3693-2007-5.ch004
OnDemand:
(Individual Chapters)
Available
$37.50
No Current Special Offers
TOTAL SAVINGS: $37.50

Abstract

Chapter 4 delves into the heart of modular programming in C++: functions. Starting with an introduction, it establishes the significance of functions in the realm of programming. The chapter systematically dissects the nuances of function prototypes and underscores the importance of function signatures and argument coercion. By elaborating on the intricacies of argument promotion, the reader gains a profound understanding of the subtleties in function calls. A categorization of functions based on return type and argument passing offers clarity and depth to the topic. The chapter elucidates the pivotal concept of scope, supported by a practical “Yes or No” program example. A deep dive into references and the distinct differences between pass-by-value and pass-by-reference paves the way for more advanced topics. Highlights include the exploration of default arguments, the power of function overloading, and the magic of recursion juxtaposed against iteration. The discussion on storage classes, with an emphasis on static storage, reinforces understanding.
Chapter Preview
Top

4.1 Introduction

Functions, which are also called methods or procedures in other programming languages, let the programmer break up a program's tasks into separate parts that can run on their own. Every program you've developed has used functions. Sometimes known as user-defined functions or programmer-defined functions, the statements in the function bodies are written just once, repeated in many places, and concealed from other functions.

There are several reasons to modularize a program using functions. One is the divide-and-conquer strategy, which makes software development more manageable by developing programs from tiny, straightforward components. The reusability of software functions as building blocks for the creation of new programs, which is another aspect. In older programs, for instance, we did not need to declare how to read a line of text from the keyboard. The getline function of the string> header file is how C++ provides this functionality. Avoiding code redundancy is a third incentive. In addition, separating a program into logical functions makes it simpler to debug and maintain.

NOTE: To make software easier to reuse, each function should only be used for a single, well-defined task, and the name of the function should make it clear what it does. These functions make it simpler to develop, test, debug, and maintain applications.

A function that does just one duty is simpler to test and debug than a function that performs several functions.

When a function is run, it either makes something happen or gives control back to the person who called it.This program's structure is comparable to the hierarchical method of management. A manager (similar to the calling function) instructs an employee (similar to the called function) to complete a job and report back (i.e., return) the results. The manager function is unaware of how the worker function completes its assigned responsibilities. Unbeknownst to the employer, the employee may also perform other employee functions. This concealment of implementation specifics improves software engineering excellence.

We have created classes that include basic functions with no more than one argument. Often, functions need many pieces of information to fulfill their duties. Now we will examine functions with numerous arguments.

Each parameter (sometimes referred to as a “formal parameter”) in the function specification must have one argument in the function call.

Transfer control back to the caller.

There are three methods to restore control to the initial execution point of a function. If the function does not return a value (i.e., it has a void return type), control returns when the program reaches the function-ending right brace or when the statement is executed. Return;.

If the function does return a result, the statement return expression; evaluates expression and returns the value of expression to the caller.

Top

4.2 Function Prototypes And Argument Coercion

A function prototype, which is also called a “function declaration,” tells the compiler the name of the function, the type of data it returns, the number of parameters it expects to get, the types of those parameters, and the order in which it expects to get parameters of the same type.

In C++, function prototypes are used to declare a function prior to its definition. A function prototype informs the compiler of the return type, name, and argument list of the function. This information is used by the compiler to verify that the right amount and kinds of arguments are given to the function during function calls.

For instance:

int add(int a, int b);

This is the function prototype for the “add” function, which takes two integers as input and returns an integer.

Coercion of function arguments is the automated conversion of an argument to the data type required by the function. For instance, if a function expects a double argument but is supplied with an int parameter, the int value will be immediately transformed to a double before being sent to the function. The term for this is type coercion.

This is an example of Argument Coercion:

void printSquare(double x) {
std::cout << x * x << std::endl;
}
int main() {
int i = 5;
printSquare(i); // argument is automatically converted to double
return 0;
}

Complete Chapter List

Search this Book:
Reset