New C Programmer Needs HELP! 
Author Message
 New C Programmer Needs HELP!

I'm a fif{*filter*}-year-old totally new to C programming and have been
reading a book recently on programming in C on the Macintosh. I pretty
much understood all the concepts until I reached the chapter on global
variables and function parameters. You might just say that I simply
don't get the point (pun intended.) I would really appreciate any
insight that anyone can give me on global variables and function
parameters. I'm really e{*filter*}d about programming in C.

So, if anyone out there is willing to take me under their wing and teach
me a few C concepts, I would really appreciate it!

-J Payne

P.S. If you're a Windows programmer, don't worry. I really don't mind.
--



Wed, 28 Jun 2000 03:00:00 GMT  
 New C Programmer Needs HELP!


Quote:
> I'm a fif{*filter*}-year-old totally new to C programming

Wow, starting early, eh? :)

Quote:
> and have been reading a book recently on programming in C on the Macintosh.

I imagine there's a lot of Mac-specific stuff in there, then. You'll want
to ignore that.

Quote:
> I pretty much understood all the concepts until I reached the chapter on
> global variables and function parameters. You might just say that I simply
> don't get the point (pun intended.) I would really appreciate any
> insight that anyone can give me on global variables and function
> parameters.

You'll need to be a little more specific than that. What about global
variables and function parameters don't you understand, exactly?

--
I believe it would at least be a violation of fair play to kill one's
opponent during a chess match, however temporarily.

--



Wed, 28 Jun 2000 03:00:00 GMT  
 New C Programmer Needs HELP!



Quote:
> I'm a fif{*filter*}-year-old totally new to C programming and have been
> reading a book recently on programming in C on the Macintosh.

Learning to program with C and programming with C on the Mac are two
conflicting tasks.  I program for both Windows and Mac (in C) and the
problem with Mac programming is that it hardly supports ANSI and Unix
concepts.  Especially for files.

However, if you are reading a book that teaches C programming on the Mac,
you may be tackling too much at once.  I do recommend reading how to
program C on either a Unix or DOS (command line, character based OS)
platforms.

Quote:
> I pretty much understood all the concepts until I reached the chapter on

global variables and function parameters.

Do you understand the use (and concept) of a stack and a heap?  Once I know
what you know, I will be able to help.
Feel free to write directly to me at my personal email:

-Arizona Stafford

Quote:
> P.S. If you're a Windows programmer, don't worry. I really don't mind.

P.S. Don't knock Windows too much.  Even though Big Brother Bill Gates and
Microsoft made it, it actually is an extremely well thought out OS (more
flexibility/extensibility than most other OS's).  Even though many of the
GUI ideas originated from Mac (Mac stole there ideas from Xerox in the late
70's and early 80's).
--



Thu, 29 Jun 2000 03:00:00 GMT  
 New C Programmer Needs HELP!



Quote:
> I'm a fif{*filter*}-year-old totally new to C programming and have been
> reading a book recently on programming in C on the Macintosh...
> ...I'm really e{*filter*}d about programming in C.

I'm more than twice your age and have just started too. I am using
Codewarrior Professional.
There is a free version, Codewarrior Lite at < http://www.*-*-*.com/ >.
It covers
Macs, Windows. The $$ versions have on-line Adobe Acrobat readable manuals.
Another good book
is "Expert C Programming - Deep C Secrets" by Peter van der Linden (
Prentice Hall
0-13-177429-8  $30 - $40). It is WAY over my head right now, but very
entertaining.
Lots of good examples and "Some light relief" stories to break up the heavy
duty stuff. I loved
the tunefs man page example.
Also try the used book department of your local college bookstore. Keep
reading the books, compare
one to another and the answers will find themselves.

jgaff
--



Thu, 29 Jun 2000 03:00:00 GMT  
 New C Programmer Needs HELP!

Quote:

> I'm a fif{*filter*}-year-old totally new to C programming and have been
> reading a book recently on programming in C on the Macintosh. I pretty
> much understood all the concepts until I reached the chapter on global
> variables and function parameters. You might just say that I simply
> don't get the point (pun intended.) I would really appreciate any
> insight that anyone can give me on global variables and function
> parameters. I'm really e{*filter*}d about programming in C.

> So, if anyone out there is willing to take me under their wing and teach
> me a few C concepts, I would really appreciate it!

> -J Payne

> P.S. If you're a Windows programmer, don't worry. I really don't mind.
> --


See if this helps.

A Global Variable basically is just a variable that can be used in any function
at all within
your main function.   They are needed because if in one function a certain value
is set to a variable, in another function that variable will not have the same
value.

So, for example if you want to write a program that takes two numbers inputted by
the user and multiply them in one function and add them in another function, you
would initiate these two variables as global variables that way both of your
functions could use them.

    Also, lets kill two birds with one stone here.  This program is also a good
example of using function parameters.  You will notice in here that the user
inputs two numbers.  We need to pass those two numbers to the functions that are
going to multiply and add those two numbers.  So what you would do is (if you
want your function that you created to return a value, which in this case we do)
set the function equal to a variable: in our case answer1 and answer2 and put the
names of the variables you are passing in the
parentheses(input_from_user1,input_from_user2).  You don't need to call them
int's in this case.  Next when you write the functions add and multiply you take
the two variables that you passed and "give them new names."  So the variables
input_from_user1 and input_from_user2 are "renamed to number1 and number2, but
only within that function.  When you write the function, you call the variables
by there "new name" and you must define what type they are.  What you do need to
do is prototype your function and this is done by  copying exactly what you typed
for the beginning of you function

int multiply(int number1,number2)

and pasting this line somewhere before the main function.  You also need to add a
semicolon when you do this.  This is called prototyping your function.
    So if you pass 3 variables to a function you must put three "new names" for
these variables in your prototype.  If you pass one int and one char to a
function, you must prototype a "new name" for one int and one char.  This should
all make more sense after you look at the program.

(This is a quick source example so there might be errors)

#include <stdio.h>
#include <stdlib.h>

int input_from_user1,input_from_user2;/*Global variables are defined here*/

int multiply(int number1,number2);/*Here is where your functions are prototyped*/

int add(int numeral1,numeral2);/*make sure to add a semicolon*/

void main(int argc,char* argv[])
{
    int answer1,answer2;
    scanf("%d",&input_from_user1)/*get the input from the user*/
    scanf("%d",&input_from_user2)/*I used scanf, but many people don't like to
use it*/

    answer1=multiply(input_from_user1,input_from_user2);/*This is where
thevariables*/
    answer2=add(input_from_user1,input_from_user2);/*are passed to the
functions*/
     /*notice that you don't need to call the variables int's in this step*/

    int multiply(int number1,number2)
/*Here is a the line you copy to prototype the function.  You need to call the
function an int because that is the type of value that it is returning: int
answer1;*/
    {you would multiply the two numbers here
    return answer1;/*This is how you return your value*/
    }

    int add(int numeral1,numeral2)/*These are the same two input variables being
passed   to this function, but we called them a different name withing this
function*/
    {you would add the two numbers here
    return answer2;
    }

Quote:
}

--



Thu, 29 Jun 2000 03:00:00 GMT  
 New C Programmer Needs HELP!

Quote:


>A Global Variable basically is just a variable that can be used in any function
>at all within
>your main function.

I fail to understand the "within your main function" part.

Quote:
>  They are needed because if in one function a certain value
>is set to a variable, in another function that variable will not have the same
>value.
>So, for example if you want to write a program that takes two numbers inputted >by
>the user and multiply them in one function and add them in another function,
>you
>would initiate these two variables as global variables that way both of your
>functions could use them.
>    Also, lets kill two birds with one stone here.  This program is also a good
>example of using function parameters.  You will notice in here that the user
>inputs two numbers.  We need to pass those two numbers to the functions that
>are
>going to multiply and add those two numbers.  So what you would do is (if you
>want your function that you created to return a value, which in this case we
>do)
>set the function equal to a variable: in our case answer1 and answer2 and
>put the
>names of the variables you are passing in the
>parentheses(input_from_user1,input_from_user2).  You don't need to call them
>int's in this case.

I fail to understand the "need to call them int's in this case" part.

Quote:
>Next when you write the functions add and multiply
>you take
>the two variables that you passed and "give them new names."  So the variables
>input_from_user1 and input_from_user2 are "renamed to number1 and number2, but
>only within that function.

A widely used conceptual model seems to be that the variables are not
renamed, but that function parameters are variables that are local
to the function, and that they are _initialized_ with the values of the
actual parameter. The difference is that if the variables were just
renamed, changes to function arguments would be reflected in the
variables "passed to" the function. What is described here more or
less describes the handling of parameters in fortran. It is highly
inadequate to explain what happens if you use an expression as an
actual parameter.

Quote:
> When you write the function, you call the variables
>by there "new name" and you must define what type they are.  What you do
>need to
>do is prototype your function and this is done by  copying exactly what you
>typed
>for the beginning of you function

Prototypes are _needed_ for variadic functions, and declarations are
_needed_ for functions not returning int, at least in contemporary C.
You _may_ prototype functions, but "need" is a bit strong.

Quote:
>int multiply(int number1,number2)

This is neither a valid declaration nor a valid definition, is it?
The parameter names can be omitted in a prototype, but _not_ their
types.

Quote:
>and pasting this line somewhere before the main function.  

Simply writing the definition before the first use makes this easier
to maintain.

Quote:
>You also need to add a
>semicolon when you do this.  This is called prototyping your function.
>    So if you pass 3 variables to a function you must put three "new names"
>for
>these variables in your prototype.  If you pass one int and one char to a
>function, you must prototype a "new name" for one int and one char.  This
>should
>all make more sense after you look at the program.
>(This is a quick source example so there might be errors)
>#include <stdio.h>
>#include <stdlib.h>
>int input_from_user1,input_from_user2;/*Global variables are defined here*/
>int multiply(int number1,number2);/*Here is where your functions are
>prototyped*/

A compiler would have detected the obvious error. If you post code examples,
it is always a good idea to try to compile them.

Quote:
>int add(int numeral1,numeral2);/*make sure to add a semicolon*/
>void main(int argc,char* argv[])

This was to be expected, but I'm getting the impression that this
_must_ be a troll. In C, there are two possible signatures for the
program entry point: "int main(void)" and "int main(int, char **)".
The above signature is no one of them.

- Show quoted text -

Quote:
>{
>    int answer1,answer2;
>    scanf("%d",&input_from_user1)/*get the input from the user*/
>    scanf("%d",&input_from_user2)/*I used scanf, but many people don't like to
>use it*/
>    answer1=multiply(input_from_user1,input_from_user2);/*This is where
>thevariables*/
>    answer2=add(input_from_user1,input_from_user2);/*are passed to the
>functions*/
>     /*notice that you don't need to call the variables int's in this step*/
>    int multiply(int number1,number2)
>/*Here is a the line you copy to prototype the function.  You need to call the
>function an int because that is the type of value that it is returning: int
>answer1;*/
>    {you would multiply the two numbers here
>    return answer1;/*This is how you return your value*/
>    }

Now, this is a function definiton within a function. This is not allowed
in contemporary C, and was not a valid C construct in the quasi-standardized
language that was informally defined by K&R1. This is both wrong and shows
a basic lack of knowledge about the C programming language.

Defining main() with a "void" return type shows that you have not been
reading the language definition, or a newsgroup that deals with the
C programming language. Definig functions in the body of main() proves
that this either is a not too well designed troll, or that you don't
know what you are talking about at all. For the "horrible" wrong
definition of main(), one can argue that it is wrong from a technical
point of view, but that some implementations of the C programming
language accept that construct and give it a well-defined meaning.
I did not yet come accross an implementation fo the C programming
language that did allow _this_ construct.

Quote:
>int add(int numeral1,numeral2)/*These are the same two input variables
>being
>passed   to this function, but we called them a different name withing this
>function*/
>    {you would add the two numbers here
>    return answer2;
>    }
>}

Kurt

--
| Kurt Watzka                             Phone : +49-89-2178-2781

--



Thu, 29 Jun 2000 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. New Programmer, Needs Help

2. New programmer needs help

3. Need Help for new programmer

4. New C programmer needs help...

5. New C Programmer needs help!

6. New C Programmer Needs Help

7. new C programmer needs help with quicksort

8. New programmer needs help

9. newbe/cs student, need help w/ code

10. US-NY-New York-Programmers needed

11. New MVC++ programmer needs info

12. [Fwd: New MVC++ programmer need info]

 

 
Powered by phpBB® Forum Software