Where Do I Put Functions In iOS App Code?

Matthew Campbell, July 27th

This is a question that I get often from readers and participants in our online workshops. The problem is usually that a function does not work or is not available for use.

For example, a reader recently sent me this code in an email:

The big problem above is that the reader was trying to code a function from within the main function. This is not really how it works, you need to make sure to have the function declared on it’s own (not inside any other function) and the function must be declared BEFORE you use it (more on that in a bit). So, to make this code work it would need to look more like this:

Note that I also moved the printf statement back into the main function here. While the above approach works the typical place to put C functions in situations like this is in a different files.

As you can imagine programs that are used with iOS apps are very complex so you will want to start keeping entities like functions and classes in separate files so that you don’t blow a braincap. If you have simply C functions like the one above you can put them into normal c files. You will actually need two files: a header file and a code file.

Code files have all the code that declares the function, here this would be this code:

Header files have what are called forward declaration

Forward declarations are the very first part of the function declaration and they are used so that we can reference the function even before it is declared in the code file.  The forward declaration for our function would look like this:

Notice that the forward declaration ends in a semi-colon (;).  So, the function is declared first in the forward declaration in the header file and then implemented in the code file.  One more thing, your code file will need to include the header file for this to work.  So you complete header file for this function would look like this:

Once you have coded your functions over these two files (the .h header file and the .c code file) then you can use your functions anywhere in your program.  All you need to do is include the header file in the top of the file where you want to use the function.  Then you can simply use the function as if the function was coded right in the same file.  Obviously, if you have hundreds of functions then you would need to do this.

NOTE: Functions are available for you to use in iOS programming, but I want to emphasize here that the object oriented approach to programming is the more generally used approach in iOS app.  Objects in object oriented programming can use functions but they usually use something very similar called methods to break up code into smaller chunks.

Last week I released a new online workshop that will show you how to do programming, check out the video below for all the details on this new special product.

Click here or the box to the right to access to your online workshop now…

Each lesson comes packed with comprehensive video, source code and text. When appropriate I include hands-on exercises. Check out the list below to see what is specifically covered in each lesson:

- Lesson 1 – Overview of iPhone OS
- Lesson 2 – Introduction to Tools: XCode, Interface Builder & iPhone Simulator
- Lesson 3 – Your First App
- Lesson 4 – Super-Charge XCode

- Lesson 1 – What is Programming?
- Lesson 2 – C Programming Basics and Specifics
- Lesson 3 – Functions
- Lesson 4 – Variables and Arrays
- Lesson 5 – Program Flow
- Lesson 6 – Loops
- Lesson 7 – Complex Data with Struct
- Lesson 8 – Putting It All Together

- Lesson 1 – What is Object Oriented Programming?
- Lesson 2 – Objects
- Lesson 3 – More Strings, Lists and the For Each Loop
- Lesson 4 – Memory Management
- Lesson 5 – Designing Your Own Classes
- Lesson 6 – Extending Classes With Categories
- Lesson 7 – Protocols & Key-Value Coding

- Lesson 1 – Overview of Cocoa-Touch + Model-View-Controller
- Lesson 2 – Using Interface Builder (The View)
- Lesson 3 – Target-Action and the View in Code
- Lesson 4 – Delegation
- Lesson 5 – Super-Charging Your View With Interface Builder
- Lesson 6 – Model & App Architecture

Click here or the box to the right to access to your online workshop now

You must be logged in to post a comment.

  • Hours of videos, demos & screen casts
  • SOURCE CODE
  • Detailed HANDS-ON Exercises
  • Much Much Much More
  • UIKit
  • Objective-C
  • Core Data
  • More!
  • Learn How To Make An iPhone App right now using my proven system.

    PS: Tons of us are having a blast living off our own code, join us now. Click here to get all the details.