TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: David Burns
from: Darin McBride
date: 1998-07-11 20:53:26
subject: Re: Beginner`s Questions

SA> char main() returns a char.
SA> char *main() returns a pointer to a string.

 DB> When you say it returns nothing, how do a make a 
 DB> procedure return something?  and how do i access that returned value?

Say you have a function vfoo:

void vfoo(void)
{
  /* code */
}

This function doesn't _return_ anything.  Now, say ifoo:

int ifoo(void)
{
  int i = 0;
  /* code that may manipulate i */
  return i;
}

This function returns i.  i is, as we can see, an int.  This is perfect as
ifoo() is declared to return an int (the int in front of ifoo(void)).  So
we return i in the last line.  (NOTE: there must be a 'return' in the last
line of a non-void function, but there can be other 'return's in the
function.  Some say this is 'poor programming', others find no problem with
it - so I say use it with caution.  Often you are better off if you only
have one return statement per function.)

Now say lfoo:

long lfoo(void)
{
  long l = 0;
  /* code that may manipulate l */
  return l;
}

Now you want bar to use these functions and use their values:

void bar(void)
{
  /* variables we will use in this function: */
  int ifoo_rc = 0;
  long lfoo_rc = 0;

  vfoo(); /* no return code from this */
  ifoo_rc = ifoo(); /* store the return value - which was 'i' inside ifoo -
                     * in our local variable 'ifoo_rc' */
  lfoo_rc = lfoo(); /* do the same with lfoo. */

  /* note: bar returns 'void', so we don't need a return, but we can: */
  return;
}

 DB> Can you explain the use of '*' in variables?

It means "pointer to" or "pointed at by", depending on
its context.  For example:

char* variable;

Here it says (the rule is to read it from the variable 'out' - in this
case, the rule simplifies to 'read it backwards' - to find the full set of
rules, see PTRTUTOR.TXT in the snippets):

'variable [is a] pointer to char'.  That is, the variable holds the address
of a char (or an array of chars).

Another example:

int* foo(void);

This reads:

'foo [is a] function [taking] void [returning] pointer to int'.

I really suggest going to the snippets for a better (more complete) set of
examples.  :-)


---
* Origin: Tanktalus' Tower BBS (1:250/102)
SEEN-BY: 396/1 622/419 632/371 633/260 267 270 371 634/397 635/506 728
SEEN-BY: 639/252 670/213 218
@PATH: 250/102 201 99 396/1 633/260 635/506 728 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.