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:
#include <stdio.h>
int main (int argc, const char * argv[]) {
return 0;
}
int getNumber(int thisNum){
thisNum++;
return thisNum;
printf("get number:%\n", getNumber(100));
}
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:
#include <stdio.h>
int getNumber(int thisNum){
thisNum++;
return thisNum;
}
int main (int argc, const char * argv[]) {
printf("get number:%\n", getNumber(100));
return 0;
}
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.
Header Files And Code 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:
int getNumber(int thisNum){
thisNum++;
return thisNum;
}
Header files have what are called forward declaration
Forward Declarations
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:
int getNumber(int thisNum);
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:
#include "functions.h"
int getNumber(int thisNum){
thisNum++;
return thisNum;
}
How To Use Your Functions
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.
PS: Here Is How To Learn More About Programming For iOS
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.
PSS: You have two more days to use the special discount code GETITNOW to get 25% off your online workshop.
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:
Module 1 – Getting Started With iPhone App Development
- 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
Module 2 – Learn How to Program in C
- 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
Module 3 – Master Object Oriented Programming With Objective-C
- 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
Module 4 – No-BS Cocoa-Touch With iPhone SDK
- 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 log in to post a comment.