Posts categorized “Programming”.

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

[XNA] Live Lecture on Thursday

[Live Lecture]

It’s that time again folks! Lectures about programming featuring me. This Thursday, 11/13/2008, I will be having a live lecture at 7pm CST. If you’d like to participate, please contact me either here, or via email at admin@redsatori.com. I will need your email address in order to send the DimDim link to you. I start promptly at 7pm CST, although I will be there a bit earlier than that.

Additionally, please check this post again for additions to the session. I will include some assets for you to use in your own follow-along projects, or you can use them elsewhere. They most likely won’t be anything special as I am not a graphic artist, just a programmer.

If you plan on participating, please be aware that I tend to go through the content of the session pretty quickly. I don’t do this on purpose, but have a tendency to be doing about 500 things at any given time. Also, please make sure you have a working copy of Visual Studio and also XNA. You can use VS 2005 with XNA 2.0 or you can use VS 2008 with XNA 3.0 CTP (I use the latter), and they should work fairly similarly. I will double check some of my code in XNA 2.0 once I get a chance to get my desktop back up and running.

[VSDD]

This very simple design document contains the information about the project we will be working with during the live session.

Name: Bomb Guys

Objective: Objective is to not allow any bomb guys to reach your base.

Gameplay: Mouse controls the aiming reticule. Left mouse button fires.

Other things to work on: Text Rendering Engine, Particle Engine, Animation Engine (for bomb guys)

[Conclusion]

Thanks for wanting to be a part of this process. Again, if you’d like to join the session either email me at the address listed above or post it in the comments section here. Please make sure you let me know if you are  a DeVry student or a random passerby.

Thanks,

~Jim

XNA Tutorials

Sorry about the delay in getting tutorials posted here on my blog. I am hoping to get back into posting new content in the next week or so. I will be unavailable the week of November 18th, as Mines of Moria is being released that day and I really want to take a look at the new content.

That being said, I will be making a tutorial on creating 2D game utilities in the upcoming month or so. It will be a side-scrolling based engine that I will be creating which will allow multi-part animations and a basic scripting language for this type of game. Additionally, I will be posting tutorials on a simple particle engine, a cursor engine, and a couple of other graphical engines that may be of use to some of you. At the very least, you can see how the code works in order to develop your own strategies.

Stay tuned for more information.

~Jim

P.S. I am also hoping to get a video podcast on here about some of what I am going to be doing as well. I don’t know how long that is going to take, but hopefully not too long.

Getting ready for a new session…

I am starting a new session at DeVry University. The course I am taking this session is GSP130: System Architecture and Assembler. I will post some nifty assembly code if I can find the time. Otherwise I will stick mostly to creating more tutorials for XNA and other languages/frameworks.

If you are coming to this blog from the GSP program at DeVry, I welcome you. I also apologize for the delays in getting everything up and running smoothly. Give me a little bit and things will look right.

~Jim