Amazon.com Widgets 2 basic questions regarding functions and printing.
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 16, 2014, 04:26:30 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Newbie Corner
| | |-+ 2 basic questions regarding functions and printing.
Pages: [1] Go Down
Print
Author Topic: 2 basic questions regarding functions and printing. (Read 609 times)
CodedLogic
Newbie
*
Posts: 5


Email




on: September 30, 2011, 03:24:22 PM

Hi everyone,

I have two questions two ask. The first one regarding functions and the second one relates to printing.

Ok so I am reading the online eBook from CocoaLab, BecomeAnXcoder. I am a noob to programming and after reading through a few books this seems to work the best for me, so I'll be using two examples from the book.

The numbers in the comments within the code are used in the book to refer to examples.

Example 1:
------------------------------
Here the author tries to explain that the variable 
Code: (Objective-C)
theRadius
will pass on it's value to the function
Code: (Objective-C)
circleArea()
. And that a second variable within the function is declared,
Code: (Objective-C)
theArea
, in order to store the value of the calculation, which is then supposedly returned.

Code: (Objective-C)
/[5]
circleArea(float theRadius) // [5.1]
{
    float theArea;
    theArea = 3.1416 * theRadius * theRadius;  // pi times r square   [5.4]
    return theArea;
}

My question is I see that the variable
Code: (Objective-C)
theRadius
is declared, but I don't see a value defined for it. I am assuming that after
Code: (Objective-C)
3.1416 * theRadius * the Radius
, which is equal to
Code: (Objective-C)
theArea
,
Code: (Objective-C)
theArea
is then returned to
Code: (Objective-C)
(float theRadius)
, which is then passed on to the function
Code: (Objective-C)
circleArea()
.  So essentially the value of
Code: (Objective-C)
theArea
is just Pi multiplied twice to a variable that doesn't have a defined value, which is the variable
Code: (Objective-C)
theRadius
.

Is this correct?
Sorry if that was a really redundant explanation.

My second question is a lot simpler, I just want to know the difference between
Code: (Objective-C)
%i %d and %f
.

I know that %f is used for float, and that %i is for intergers, but in the book he uses %d? He says d is short for decimals, but he seems to be using them for integers.

See:
Code: (Objective-C)
//[5]
int x, integerToDisplay;
x = 1;
integerToDisplay = 5 + x;
NSLog(@"The value of the integer is %d.", integerToDisplay);

why is %d used instead of %i? and are there any other % with letters that I should know of?

Thank you very much for all your help in advance and sorry if the post comes out looking weird and for any redundant explanations!

Thanks again,

- Danny


Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #1 on: October 01, 2011, 10:50:12 AM

%d and %i are the same, they're both used for printing integers.  Heres the term "decimal" refers to base 10.  For example, %x prints a value in hexadecimal (base 16) format.

The value of theRadius is set when the function is called.  It is set to the argument passed to the circleArea function (e.g., if you write circleArea (2.5) then the value of theRadius will be set to 2.5 when the function is called).

By the way, the function needs to have a return type declared; e.g.

Code: (Objective-C)
float theArea (float theRadius)
{
   ...
}


Cheers,

Steve
Last Edit: October 01, 2011, 10:51:57 AM by skochan Logged
CodedLogic
Newbie
*
Posts: 5


Email




Reply #2 on: October 03, 2011, 02:31:41 PM

Thanks for the reply Skochan.

I am sorry if I still misunderstand, but is theRadius a dummy variable, and usually I would just put a number inside the function instead of a variable?

Thanks again,

- Danny
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #3 on: October 03, 2011, 08:58:25 PM

It is a dummy variable, or more commonly known as a formal parameter name.  The actual value passed into the function when it's called is stored inside that parameter.

Steve
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.