Arguments and Local Variables
In Chapter 5, “Program Looping,” you developed programs for calculating triangular numbers. Here you define a function to generate a triangular number and call it, appropriately enough, calculateTriangularNumber
. As an argument to the function, you specify which triangular number to calculate. The function then calculates the desired number and displays the results. Program 13.4 shows the function to accomplish the task and a main
routine to try it.
#import <Foundation/Foundation.h>// Function to calculate the nth triangular numbervoid calculateTriangularNumber (int n){ int i, triangularNumber = 0; for ( i = 1; i <= n; ++i ) triangularNumber += i; NSLog (@"Triangular ...
Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.