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.
Program 13.4.
#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 number %i is %i", ...
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