Thread: Return Statement

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19

    Return Statement

    Ive have been reading several books regarding this topic, and all books state that the return statement can only return one value of result.

    How does this happen when you can return something like this to the user:


    *********
    * 222222 *
    *********

    Is that above a single result?

    Also, I see some programs having the void function like this:

    int
    main(void)
    {
    // Block of Code
    }

    How can this be void when it prints something on the screen?

    Okay, here is a code from the book:

    Code:
    void
    sperate(double num, char *signp, int *wholep, double *fracp)
    {
              double magnitude;
    
              if(num < 0)
                        *signp = ' - ';
              else if(num == 0)
                        *signp = ' ';
              else
                        *signp = ' + ';
    
              magnitude = fabs(num);
              *wholep = floor(magnitude);
              *fracp = magnitude - *wholep;
    }

    Now, in the above code, it says void, meaning this function does not return a result??? but it does, doesnt it? That what im getting confused about.
    Last edited by Daveo; 11-06-2004 at 04:44 PM.

  2. #2
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Also, im having a hard time understanding the basics of pointers. Could you guys direct me to the right place, possibly a tutorial on pointers that is not hard, and easy to understand for someone like me who is a novice at C.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is there any difference between
    Only in your mind.
    My best code is written with the delete key.

  4. #4
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Quote Originally Posted by Daveo
    Also, im having a hard time understanding the basics of pointers. Could you guys direct me to the right place, possibly a tutorial on pointers that is not hard, and easy to understand for someone like me who is a novice at C.

    I'm surprised no-one's mentioned that there is an tutorial on pointers in the FAQ's.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Jez
    I'm surprised no-one's mentioned that there is an tutorial on pointers in the FAQ's.
    You mean there is a FAQ here?!

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Quote Originally Posted by quzah
    You mean there is a FAQ here?!

    Quzah.
    LOL ... i hope that was a sarcastic comment


    here's the pointers tutorial from the FAQ. And www.google.com would be your best in finding other tutorials
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Functions take this format:
    Code:
    return_value function_name(parameter_type_1 parameter_name_1, parameter_type_2 parameter_name_2)
    The return value is useful for something like this:

    Code:
    float ratio = sine(35);
    the value that the sine function returns would be assigned to the ratio variable. Programs can do this too (and should, in C). It's is generally accepted that returning 0 is a sign that the program ran sucessfully, and other values can be used to denote cetain errors, but that's all OS dependant. Some people use void as the return type of main, and while this should be done when appropriate in user-defined functions, it is not correct usage in main.

    The function name... well you don't need an explanation on that.

    And then between the parentheses are parameters that the function needs. In my example, it's the angle measure, and then maybe you'd have another number that would denote the unites you're using, degrees or radians.

    Hope that helps for functions. As for pointers...

    A pointer is just like any other variable, except that instead of holding a number to represent a character (like char), pointers hold numbers that can be used as address for computer memory. When you first learn about them, pointers can be confusing because at the point, you have no need for pointers. As you learn more C, you will see examples of when to use pointers, and they'll make more sense to you. Let me try and give you a taste of it now:

    If you have an array of characters - a string, and you want to sort all the words in it by alphabetical order or something. You'd probably want to make a separate function that accepts the string as a parameter, processes it, and returns the result. Its pretty tricky moving whole strings and large data structures around in C, and so you use pointers. In this example, with the array of chars, you just declare a pointer, and set it equal to the address of the first element of the string (your book or tutorial on C should show you how to do this), and then you pass that pointer to your function. The function can then "dereference" that pointer to get the value it points to. Now it has the first character. It can increment the pointer, so that it points to the next space in memory, and dereference it again - now it has the first 2 characters. By repeating this process over and over, it can read the whole string.

    I'm sorry if this is confusing, but having read this, just go back to wherever you're learning C from, and keep this example in mind - it'll start to make sense.

  8. #8
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Thanks Sean!

    Now, is there any difference between:

    pointer type* pointer variable name;

    and

    pointer type *pointer variavle name;

    The difference is that the '*' is in a different position.

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    It's the same thing, some people prefer to use it one way while others prefer the other way.

  10. #10
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    .

  11. #11
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Code:
    void
    sperate(double num, char *signp, int *wholep, double *fracp)
    {
              double magnitude;
    
              if(num < 0)
                        *signp = ' - ';
              else if(num == 0)
                        *signp = ' ';
              else
                        *signp = ' + ';
    
              magnitude = fabs(num);
              *wholep = floor(magnitude);
              *fracp = magnitude - *wholep;
    }
    This line follows from the code above:

    The function type is void as it is for functions returning no result, and the function body does not include a return statement to send back a single value.

    Wtf is that meant to mean? When you make a function call to sperate, it returns results, through *signp, *wholep & *fracp, but the sentence above states the contrary.

  12. #12
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Also from the above code, how does *signp, *wholep, & *fracp know when to store results when it doesnt have the address. I cannot see anywhere in the code where &num is used to store the address.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The function is not actually RETURNING those results. It's making changes to the variables you pass to it, so it's giving you results, but it's not doing it by means of a return statement.

    As for your second question, the right sides of those equations (whether it be a function call or an equation) are probably all going to end up being memory addresses. My guess is that floor's return type is either a pointer or a memory address.

  14. #14
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    This matter gets pretty tricky for new programs. I'll show you two simple programs that calculates the sum of two numbers using both methods . That is , returning a value and using a pointer.

    Functions returning a value :
    Code:
    #include <stdio.h>
    
    int sum(int , int);
    
    int main(void)
    {
       int num1, num2, result;
    
       printf("Enter the first number : ");
       scanf("%d", &num1);
    
       printf("Enter the second number : ");
       scanf("%d", &num2);
    
       result = sum(num1, num2);
    
       printf("%d", result);
    
       return 0;
    }
    
    int sum(int x, int y)
    {
       int result;
       result = x + y;
       
       return result;
    }
    First of all we declare\write our functions according to this prototype :

    Code:
    type name_of_function(type variable);
    type : here is where you declare the value the function should return as a result , it can be an int , double, char or whatever type you want your function to return.

    name_of_function : a name specified by the user , you can choose any name (within the naming rules) but we usually name it according to its job.

    variable : this variable is what we call an argument , it takes the value sent from any other function to use it within it-self.


    don't get it ? keep reading ...

    First , we declared our function sum (you can name it as you wish btw) as an int wich means it returns an integer (we'll get to this) , between the parentheses we're telling the computer how many and what kind of arguments our sum function will take (or point to). We declared two int variables without the necissity of specifing their names.

    Now since function main won't take any arguments * , it should be declared as int main (void) where int is the type of the value that main will return. main returns a value of 0 to report a successful execution.

    * If you don't know main can take arguments then just keep using 'main (void)' untill you learn how to get a clear image of it.

    Within main , we declared 3 int variables , 2 to store inputs and one to store the return value of the function sum.

    the line : result = sum(num1, num2)

    executes sum(num1,num2) first , it sends the values of num1 and num2 to the two int variables in function sum so they are stored in the order they were sent in. You can imagine it bieng like that :

    Code:
    main    :     num1          num2
                     |           |
                     V           V
    sum     :        x           y
    note that our functions must take as many values (aka:arguments) as it can hold. In our case , we declared sum with two variables so 'main' MUST send two arguments or else you'll get an error. Also the name of the variables in our costum functions' declarations (like sum) doesn't need to match the ones we're sending from any other functions. For example , in our program , we sent the value of two variables (as arguments) which are called 'num1' and 'num2' in main , to another two variables called 'x' and 'y' in function 'sum' . Its just passing the value (or adress in case of pointers) not the variable itself.


    within the sum function we declare an int variable called result to hold the sum of x and y as shown. the 'return result' statement would return the value of 'result' to function main again.

    For example , lets say we took 5 and 6 as inputs from the user , they would get stored in their variables (num1 and num2) , sent to function sum as arguments , stored in x and y , get summed and stored in our variable 'result' wich will be 11 in this example.

    Now function sum will simply return the value 11 to the function that called it wich is 'main' here , it'll simply replace the :
    Code:
    result = sum(num1, num2);
    with :
    Code:
    result = 11;
    This is what returning a value mean.


    Functions using pointers :

    You need to understand the concept of pointers in order to understand this.

    We can let pointers declared in other functions point to memory adresses in function main (or any other function we use). We can write the previous program using pointers , without returning a value , as the following :

    Code:
    #include <stdio.h>
    
    void sum(int *);
    
    int main(void)
    {
       int result;
    
       sum(&result);
    
       printf("%d", result);
    
    
       return 0;
    }
    
    void sum(int *x)
    {
       int num1, num2 , sum;
    
       printf("Enter the first number : ");
       scanf("%d", &num1);
    
       printf("Enter the second number : ");
       scanf("%d", &num2);
    
       sum = num1 + num2;
    
       *x = sum;
    
    }
    since 'x' is pointing to the adress of 'result' , it will be able to manipulate\store the value of the variable 'result' (wich it actually does here in our example) thus we don't need to return a value to main because 'result' can be manipulated by its pointer 'x' , so we don't need to send back a value to main.


    notes :
    1. Only pointers within our function declaration (that is , the pointers declared between parentheses) are able to point at adresses of arguments from other functions. Trying to point to a variable in function 'main' from a pointer within the body any other function would lead to an error.

    2. function 'sum' returns a value to 'main' , 'main' returns a value to the computer.

    3. 'return 0' tells the computer that the program has successfly ended.



    I hope this clears things up..
    Last edited by Brain Cell; 11-06-2004 at 11:50 PM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  15. #15
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Trying to point to a variable in function 'main' from a pointer within the body any other function would lead to an error.
    Do you mean:

    Trying to point to a variable in function 'main' from a pointer within the body would lead to an error.
    If its not, im confused.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM