Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 17, 2018, 08:06:09 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Understanding the word "Constant"  (Read 6328 times)
barrettlikespizza
Newbie
*
Posts: 17


The secret of getting ahead is getting started.


WWW
« on: February 25, 2015, 10:38:32 AM »

I need help understanding what the author really means by "constant expression," "constant values," "constant integer value."

I understand what "expression, value, integer" mean on their own.
But when the word constant is added, I get confused.

I'm attaching a chart I made to help illustrate my point.

The chart lays out the different variable types.
Hopefully someone can explain how "constant" fits into this family of concepts?

Looking at my graph, would constant be a massive circle that everything else fits into? Or are constants a variation of each variable type? For example:

There are "In n Out Burgers" vs. "In n Out Burgers - Animal Style"

There are "Char variables" vs. "Constant Char variables"?

Thank you to anyone who can see where my brain is stuck and can help me out of it!
Logged
BrianLawson
Sr. Member
****
Posts: 262


Email
« Reply #1 on: February 25, 2015, 11:21:51 AM »

Declaring a variable constant removes its variability, the value assigned at declaration time is the only value it will ever hold. The constant declaration tells the compiler that the value will never change.

A function can also be a constant, that is a guarantee that the function/method will not change the value of any external variables. Declaring the arguments to a function as constant is a guarantee that the function will not change the values of those arguments.

This might help: http://stackoverflow.com/questions/5598703/c-const-usage-explanation.
Logged
barrettlikespizza
Newbie
*
Posts: 17


The secret of getting ahead is getting started.


WWW
« Reply #2 on: February 25, 2015, 07:25:13 PM »

Brian,

As always, your response is appreciated, although I am finding it hard to digest mentally.

Please tell me where my understanding is correct/incorrect:

You're saying a constant variable is a variable that cannot be changed. Like writing with a sharpie marker versus a pencil?

Questions
1.) If I want to deem a variable to be a constant, would I write that in the implementation section, the program section, or somewhere else altogether?

Please consider the basic code I've entered below.

2.) If I wanted to, I could set the variable "broccoli" in the implementation section permanently equal to 5, or 12000, or anything else?
3.) And same with the "set" methods? So I could make a constant method that says "Broccoli will always equal 12000 no matter what the program section tries to do."

4.) What would be a tangible example of why I would want to do this? is there any chance you could throw a constant variable or method into my code below and show me how constants would look in it? I think that might help me understand more clearly!

Thank you, you've already been a huge help to me!

Code: (Objective-C)

#import <Foundation/Foundation.h>

@interface Meal : NSObject

-(void) setCarrots: (int) c;
-(void) setBroccoli: (int) b;
-(void) print;
-(int) getCarrots;
-(int) getBroccoli;

@end

@implementation Meal

{
    int carrots;
    int broccoli;
}

-(void) setCarrots:(int)c

{
    carrots = c;
}

-(void) setBroccoli:(int)b

{
    broccoli = b;
}

-(void) print

{
    NSLog(@"My dinner inclues %i carrots and %i pieces of broccoli.", carrots, broccoli);
}

-(int) getCarrots

{
    return carrots;
}

-(int) getBroccoli

{
    return broccoli;
}


@end


/* program section */

int main (int argc, char * argv[])

{
    @autoreleasepool
    {
        
        Meal *Breakfast = [[Meal alloc] init];
        Meal *Lunch = [[Meal alloc] init];
        
        
        [Breakfast setCarrots: 2];
        [Breakfast setBroccoli: 1];
        
        [Lunch setCarrots:15];
        [Lunch setBroccoli: 7];
        
        NSLog(@"I ate %i carrots for lunch, and %i pieces of broccoli for breakfast. I wish I'd eaten %i burritos instead.", [Lunch getCarrots], [Breakfast getBroccoli], 15);
        
    }
    return 0;
}

« Last Edit: February 25, 2015, 07:28:34 PM by barrettlikespizza » Logged
BrianLawson
Sr. Member
****
Posts: 262


Email
« Reply #3 on: February 25, 2015, 11:54:13 PM »

Quote
You're saying a constant variable is a variable that cannot be changed. Like writing with a sharpie marker versus a pencil?
Yes, that is exactly right.

Rather than using your code, let me give you a very forced example, there are better ways to do this but it may help illustrate the point for you.
Code: (Objective-C)
#import <Foundation/Foundation.h>

@interface Circle: NSObject

-(void)setRadius:(float) r;
-(float)getRadius;
-(float)circumference;

@end

@implementation Circle
{
float radius;
}

-(void)setRadius:(float) r {
radius = r;
}

-(float)getRadius {
return radius;
}

-(float)circumference {
float const pi = 3.14159;
return 2.0 * pi * radius;
}


@end

// Program section

int main(int argc, const char * argv[]) {
@autoreleasepool {

Circle *C = [[Circle alloc] init];

float r = 5.2;
[C setRadius:r];
NSLog(@"The circumfrence of a circle of radius %3.1f is %3.2f", r, [C circumference]);
}
return 0;
}

In reality, I would use the PI constant defined by Xcode or make one of my own by defining a macro: #define PI 3.14159 then it could be used throughout the class without having to redefine the constant variable each time, i.e., in a method to calculate the circle's area.

A more useful purpose for the const is in protecting arguments to functions that are passed by reference and you want to insure that the contents pointed to by the argument cannot be changed. (float)area(const float &r) { return PI * (*r) *(*r); }

Here is the same code with an area function to illustrate the constant argument:
Code: (Objective-C)
#import <Foundation/Foundation.h>
#define PI 3.14159

@interface Circle: NSObject

-(void)setRadius:(float) r;
-(float)getRadius;
-(float)circumference;

@end

@implementation Circle
{
float radius;
}

-(void)setRadius:(float) r {
radius = r;
}

-(float)getRadius {
return radius;
}

-(float)circumference {
float const pi = 3.14159;
return 2.0 * pi * radius;
}


@end

// Program section

float area(const float *r) { return PI * (*r) *(* r); }

int main(int argc, const char * argv[]) {
@autoreleasepool {

Circle *C = [[Circle alloc] init];

float r = 5.2;
[C setRadius:r];
NSLog(@"The circumfrence of a circle of radius %3.1f is %3.2f", r, [C circumference]);

NSLog(@"The area of the circle is %3.2f", area(&r));
}
return 0;
}

If you add to the area function something like *r = 9.0; you'll see that the compiler will give you the error, "Read-only variable is not assignable."

Constants are a way for you to tell the compiler that certain variables must not be changed so that it can tell you when the code tries to make a change to them. It helps prevent bugs. Smiley
Logged
Kaikagaga
Newbie
*
Posts: 1


Email
« Reply #4 on: April 16, 2018, 08:01:42 PM »

This topic is most useful.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.