Posts categorized “C++”.

Anatomy of a C++ Function

[Anatomy of a C++ Function]

In C++, functions are required to make your code scalable, extensible and easy to maintain. in this post, we will be discussing the anatomy of a function. 

Functions contain three main parts: Prototype, Header, and Definition. Each of these is important in their own right, and together they create usable code that your applications can use during execution.

[Prototypes]

Function prototypes have a specific structure. They require the following parts:

 

  • Return type
  • Name
  • Parameter list

 

The return type is the type of data being returned from the function, either void or int or something else entirely. The name of the function is the name that will be used to differentiate it from other functions in the program and the parameter list is a series of variable types which will be passed to the function at run-time. Here is what a function prototype looks like:

<return type> functionName(<parameter list>);

We can make a simple function as a demonstration:

int double(int);

As you can see, the function prototype we just created contains a return type, a name and a parameter list (in this case a single parameter.)

[Headers]

Function headers are nearly identical to prototypes, with a couple of exceptions. One of the main differences is that you need to qualifiy the parameter list. What this means is that you have to give a variable name for each variable in the list. Additionally, for class-specific functions, you need to also include the scope operator. Also, since the header comes directly above the function definition, you will not be ending the line with a semicolon, but rather with braces. Here is the anatomy of a function header:

<return type> <class name>::functionName(int myVariable, int myOtherVariable)

{

}

Again, it is nearly identical to the function prototype, but has a bit more information in it. Here is what our function header should look like for our double function:

int double(int x)
{

It is pretty easy to move from prototype to header with very few modifications. You must declare the variable names for the variables you are passing to the function and you must make sure you have braces surrouding the actual definition section of the function.

[Definitions]

Now that you have a prototype and a header, we need to implement the function. Function implementation occurs in the definition of the function:
<return type> <class name>::functionName(int myVariable, int myOtherVariable)
{

// function definition here

}

For our example function, we would have something like this:
int double(int x)

{

return x * 2;

As you can see, the function is pretty simple. In this function we are simply returning an integer that is doubled. The return statement has to return a value that is consistent with the header and prototype return values. If we had instead tried:

int double(int x)

{

return “Hello World”;

}

Our code would have given us an error since converting from a string to an integer is pretty much impossible.

[Conclusion]

In this post, we discussed the three main parts of a function: Prototype, Header, and Definition. The prototype is placed at or near the top of the page and tells the compiler what to expect out of the function in terms of return type and parameter list. The header expands upon the prototype and declares required variables and sets the required scope. This usually comes later in the sourcefile and always directly precedes the definition. The definition is the implementation of the function. If the function is to return a value, the function must contain a return statement.

Study Group – CIS170C

I will be holding another study group this Saturday at 2pm CST. 

 

Same rules apply, if you’d like to attend, please get me your email address. I will use my list of previous attendees to send emails to everyone for this week’s study group. If you do not wish to attend, please contact me and let me know to take you off of the list.

 

~Jim

 

P.S. I will get more information posted in this thread as to what we will be covering this week.

CIS170C Study Group

Hello Everyone:

I will be hosting a study group on Saturday Afternoon at 2pm CST. It should last between 1 and 2 hours. If you would like to attend, please leave me a comment here with your email address in it. It will not be approved, so you do not have to worry about other people getting access to your email.

 

We will be covering the following topics:

  • Classes
  • Constructors and methods
  • Overloading
  • Instantiation of classes
  • Calling class methods. 

Each of these topics is important in object-oriented programming. You really will need a complete grasp of the ideas behind the code in order to be a good programmer. 

I hope to see a number of people at this study group. If you have any questions for me prior to Saturday, please feel free to email me at redsatori+school@gmail.com.

~Jim