HomePortalFAQSearchMemberlistUsergroupsRegisterLog in

Share | 
 

 Printing is cool, but dealing with numbers and stuff is where it is at, right?

Go down 
AuthorMessage
The Fellow
Admin
avatar

Posts : 100
Join date : 2010-04-28
Age : 26
Location : In my head, dreaming up how to shake up the world

PostSubject: Printing is cool, but dealing with numbers and stuff is where it is at, right?   Wed Oct 13, 2010 7:20 pm

Oh hello there! I am going to show you how to write a function to use in your main. We are going to write a program that will do a calculation then print it's answer.

First let's define a Constant:
Code:
#define NUMBER 5
A constant is a number that can be used ANYWHERE and EVERYWHERE in the program. You can only put it at the top of the program, and sadly, you can not change it's value. As you can see, all you have to do to define a constant is give it a name and a value, nice, no?

Ok, that is cool, with the global variables, but that does not mean anything to me if it is not used. So let us start writing the rest of the code:
Code:

int main(){
OH NOES! I ALMOST FORGOT TO EXPLAIN SOMETHING!!!

Ok, let me explain something, a variable! a variable is a piece of information that can change, thus why it is called a variable. The creation of a variable is simple just state what type of piece of information it is, then giving a name to the variable, and at long last defining the variable with a number, or a function that returns a number. For this, we are going to use a function, since it is what I use more often than just numbers.

now back to where we was:
Code:
int ourNum = ourFunction();
As you can see if you call a function you have to include () by it. That is so if you need to give it some number to work with or something, you can. if not, it will run the function without it.

Now, let us make your friend the printf function more crazy awesome, but telling it to print our number we got.
Code:
printf("Our number is: %d", ourNum);
ok, now what we did was do what the printf function was made for, FORMATTING! yeah, that is right, I tricked you into doing what the man wants you to do and make something do what it is suppose to in its way. MWUAHAHAHAHA!!! *coughcough* anyways, with the %d there we basically said, the piece of information that is after the ',' is what we need and it should be a number. Formatting can get REALLY weird and funky, but I will get that in a lot of later tutorials from now!

and finally
Code:
}
to end the block statement!



Oh wait! There is more! If we tried to run this now, it would never compile! It has no idea what ourFunction() is! so let us define that.
Code:
int ourFunction(){
now before we go on, let us just talk about what we did there. basically with that 'int' part we said, "Hey, you, the big scary compiler guy! yeah, you! This function returns a int, so be careful, will ya!" then we gave the function a name, and it's parameters(the pieces we of information we define inside the paraenthesis). Then started the block statement.

Now, let us manipulate us some data!
Code:

int number = NUMBER-3;
Ok, now you may be able to read this statement with what you now, but just in case. First we defined the variable number with the int type, then took our constant and subtracted 3 from it

ok, now how do we return the variable you might ask? Well, what did you just asked to do? Return? well, you can define what you want it to return by one line.
Code:
return number;
Yeah, that is all you have to put to end that, and now, all you have to do is end this statement!
Code:
}

So, for your first simply complex program you may feel a bit overwhelmed, but that is ok, you will get used to it all, and well, your programs will thank you for more practice, and debugging!



Entire code:
Spoiler:
 

Back to top Go down
View user profile http://compilecrazy.forumotion.net
Excimer Sun Software

avatar

Posts : 152
Join date : 2010-05-07
Age : 26
Location : USA

PostSubject: Re: Printing is cool, but dealing with numbers and stuff is where it is at, right?   Wed Oct 13, 2010 8:24 pm

Great tutorial, Fellow! One thing, where's the "Entire code"? Laughing
Back to top Go down
View user profile
The Fellow
Admin
avatar

Posts : 100
Join date : 2010-04-28
Age : 26
Location : In my head, dreaming up how to shake up the world

PostSubject: Re: Printing is cool, but dealing with numbers and stuff is where it is at, right?   Wed Oct 13, 2010 8:26 pm

Embarrising ESS, just click the green line
Back to top Go down
View user profile http://compilecrazy.forumotion.net
Excimer Sun Software

avatar

Posts : 152
Join date : 2010-05-07
Age : 26
Location : USA

PostSubject: Re: Printing is cool, but dealing with numbers and stuff is where it is at, right?   Wed Oct 13, 2010 8:45 pm

Oh, ok! Awesome! cheers

p.s.: How the heck did you do that?! scratch
Back to top Go down
View user profile
The Fellow
Admin
avatar

Posts : 100
Join date : 2010-04-28
Age : 26
Location : In my head, dreaming up how to shake up the world

PostSubject: Re: Printing is cool, but dealing with numbers and stuff is where it is at, right?   Wed Oct 13, 2010 8:48 pm

Lol, I will show you later
Back to top Go down
View user profile http://compilecrazy.forumotion.net
Excimer Sun Software

avatar

Posts : 152
Join date : 2010-05-07
Age : 26
Location : USA

PostSubject: Re: Printing is cool, but dealing with numbers and stuff is where it is at, right?   Wed Oct 13, 2010 8:54 pm

Ohhh, alriiggghhht...
Back to top Go down
View user profile
Sponsored content




PostSubject: Re: Printing is cool, but dealing with numbers and stuff is where it is at, right?   

Back to top Go down
 
Printing is cool, but dealing with numbers and stuff is where it is at, right?
Back to top 
Page 1 of 1
 Similar topics
-
» Dealing with large images?
» Odd numbers
» Cool Cats & Hot Dogs in Bearville!
» Create the Best Outfit using BABV PSI
» Emoticon Code from Neopets!

Permissions in this forum:You cannot reply to topics in this forum
Compile Crazy :: Programming :: C Family :: C :: For Beginners-
Jump to: