Mastery 12

Creating functions in C++

Functions

Functions allow to structure programs in segments of code to perform individual tasks.

In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is:

type name ( parameter1, parameter2, ...) { statements }

Where:
– type is the type of the value returned by the function.
– name is the identifier by which the function can be called.
– parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example:int x), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.
– statements is the function’s body. It is a block of statements surrounded by braces { } that specify what the function actually does.

 

 

// function example
 <iostream>
using namespace std;

int subtraction (int a, int b)
{
  int r;
  r=a-b;
  return r;
}

int main ()
{
  int x=5, y=3, z;
  z = subtraction (7,2);
  cout << "The first result is " << z << 'n';
  cout << "The second result is " << subtraction (7,2) << 'n';
  cout << "The third result is " << subtraction (x,y) << 'n';
  z= 4 + subtraction (x,y);
  cout << "The fourth result is " << z << 'n';
}
The first result is 5
The second result is 5
The third result is 2
The fourth result is 6

CC BY 4.0 Mastery 12 by Mauricio Cooper is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.