Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 6 / 68

Writing your own function

This lesson covers the basics of writing a function in Objective-C, with the aim of producing code you can reuse.

You define a C-style function as returnType name(args) { }. Gathering work you do repeatedly under a name means you can run it just by calling it.

In the sample code, a greet function takes an NSString *name argument and assembles a greeting with [NSString stringWithFormat:@"...", name]. Method calls written in square brackets are Objective-C's distinctive syntax.

A common early stumble is that Objective-C has two function-like things: C-style functions and object methods. Deciding which to use takes a while to settle.

In professional work, the usual split is a C-style function for a simple utility and a method for anything tied to an object.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)