1.13. Defining Functionality for Classes
Problem
You want to define some functionality for your classes and allow them to be reused later.
Solution
Create instance or class methods for your classes in order to create reusable blocks of code, or simply call a method in your program.
Discussion
Nearly every programming language creates procedures and
functions to encapsulate specific functionality,
especially functionality that the programmer uses over and over. Some
languages consider “procedure” and “function” just terms for the same
thing, while others make a distinction between them. A procedure is a
block of code with a name and an optional set of parameters. It does not
have a return value. In Objective-C, a procedure returns void to indicate it does not return a value. A
function is similar but does have a return value. Here is a simple
procedure (with an empty body) written in C:
voidsendEmailTo(constchar*paramTo,constchar*paramSubject,constchar*paramEmailMessage){/* send the email here ... */}
This procedure is named sendEmailTo and
has three parameters: paramTo,
paramSubject, and
paramEmailMessage. We can then call
this procedure as follows:
sendEmailTo("somebody@somewhere.com","My Subject","Please read my email");
Turning this procedure into a function that returns a Boolean value, we will have code similar to this:
BOOLsendEmailTo(constchar*paramTo,constchar*paramSubject,constchar*paramEmailMessage){/* send the email here ... */if(paramTo==nil||paramSubject ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access