Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

Functions

Although main() is a function, it is an unusual one. To be useful, a function must be called, or invoked, during the course of your program. main() is invoked by the operating system.

A program is executed line-by-line in the order it appears in your source code until a function is reached. Then the program branches off to execute the function. When the function finishes, it returns control to the line of code immediately following the call to the function.

A good analogy for this is sharpening your pencil. If you are drawing a picture and your pencil point breaks, you might stop drawing, go sharpen the pencil, and then return to what you were doing. When a program needs a service performed, it can call a function to perform the service and then pick up where it left off when the function is finished running. Listing 2.6 demonstrates this idea.

Listing 2.6. Demonstrating a Call to a Function

  1:  #include <iostream>
  2:
  3:  // function Demonstration Function
  4:  // prints out a useful message
  5:  void DemonstrationFunction()
  6:  {
  7:      std::cout << "In Demonstration Function\n";
  8:  }
  9:
 10:  // function main - prints out a message, then
 11:  // calls DemonstrationFunction, then prints out
 12:  // a second message.
 13:  int main()
 14:  {
 15:      std::cout << "In main\n" ;
 16:      DemonstrationFunction();
 17:      std::cout << "Back in main\n";
 18:      return 0;
 19:  }

Output

In main
In Demonstration Function
Back in main

Analysis

The function DemonstrationFunction() is defined on lines 6–8. When it is called, it prints a message to the console screen and then returns.

Line 13 is the beginning of the actual program. On line 15, main() prints out a message saying it is in main(). After printing the message, line 16 calls DemonstrationFunction(). This call causes the flow of the program to go to the DemonstrationFunction() function on line 5. Any commands in DemonstrationFunction() are then executed. In this case, the entire function consists of the code on line 7, which prints another message. When DemonstrationFunction() completes (line 8), the program flow returns to from where it was called. In this case, the program returns to line 17, where main() prints its final line.

Using Functions

Functions either return a value or they return void, meaning they do not return anything. A function that adds two integers might return the sum, and thus would be defined to return an integer value. A function that just prints a message has nothing to return and would be declared to return void.

Functions consist of a header and a body. The header consists, in turn, of the return type, the function name, and the parameters to that function. The parameters to a function enable values to be passed into the function. Thus, if the function were to add two numbers, the numbers would be the parameters to the function. Here’s an example of a typical function header that declares a function named Sum that receives two integer values (first and second) and also returns an integer value:

int Sum( int first, int second)

A parameter is a declaration of what type of value will be passed in; the actual value passed in when the function is called is referred to as an argument. Many programmers use the terms parameters and arguments as synonyms. Others are careful about the technical distinction. The distinction between these two terms is not critical to your programming C++, so you shouldn’t worry if the words get interchanged.

The body of a function consists of an opening brace, zero or more statements, and a closing brace. The statements constitute the workings of the function.

A function might return a value using a return statement. The value returned must be of the type declared in the function header. In addition, this statement causes the function to exit. If you don’t put a return statement into your function, it automatically returns void (nothing) at the end of the function. If a function is supposed to return a value but does not contain a return statement, some compilers produce a warning or error message.

Listing 2.7 demonstrates a function that takes two integer parameters and returns an integer value. Don’t worry about the syntax or the specifics of how to work with integer values (for example, int first) for now; that is covered in detail in Lesson 3.

Listing 2.7. FUNC.cpp Demonstrates a Simple Function

  1:  #include <iostream>
  2:  int Add (int first, int second)
  3:  {
  4:      std::cout << "Add() received "<< first << " and "<< second << "\n";
  5:      return (first + second);
  6:  }
  7:
  8:  int main()
  9:  {
 10:      using std::cout;
 11:      using std::cin;
 12:
 13:
 14:      cout << "I'm in main()!\n";
 15:      int a, b, c;
 16:      cout << "Enter two numbers: ";
 17:      cin >> a;
 18:      cin >> b;
 19:      cout << "\nCalling Add()\n";
 20:      c=Add(a,b);
 21:      cout << "\nBack in main().\n";
 22:      cout << "c was set to " << c;
 23:      cout << "\nExiting...\n\n";
 24:      return 0;
 25:  }

Output

I'm in main()!
Enter two numbers: 3 5

Calling Add()
In Add(), received 3 and 5

Back in main().
c was set to 8
Exiting...

Analysis

The function Add() is defined on line 2. It takes two integer parameters and returns an integer value. The program itself begins on line 8. The program prompts the user for two numbers (line 16). The user types each number, separated by a space, and then presses the Enter key. The numbers the user enters are placed in the variables a and b on lines 17 and 18. On line 20, the main() function passes the two numbers typed in by the user as arguments to the Add() function.

Processing branches to the Add() function, which starts on line 2. The values from a and b are received as parameters first and second, respectively. These values are printed and then added. The result of adding the two numbers is returned on line 5, at which point the function returns to the function that called it—main(), in this case.

On lines 17 and 18, the cin object is used to obtain a number for the variables a and b. Throughout the rest of the program, cout is used to write to the console. Variables and other aspects of this program are explored in depth in the next lesson.

Methods Versus Functions

A function by any other name is still just a function. It is worth noting here that different programming languages and different programming methodologies might refer to functions using a different term. One of the more common words used is method. Method is simply another term for functions that are part of a class.

  • + Share This
  • 🔖 Save To Your Account