Return Statement

This is a discussion on Return Statement within the C Programming forums, part of the General Programming Boards category; Ive have been reading several books regarding this topic, and all books state that the return statement can only return ...

  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 03: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
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  4. #4
    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.

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

  6. #6
    ---
    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.

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

  8. #8
    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.

  9. #9
    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.

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  11. #11
    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 10:50 PM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  12. #12
    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.

  13. #13
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Also Brian, the way use have used prototypes is different from what I use.

    Your way is like this:

    Code:
    int sum(int, int);
    My way - they way I have been taught:

    Code:
    int sum(int x, int y);
    Are they the same? I like your way, its shorter.

    Also, say I make the function call:

    Code:
    result = sum(4, 5)
    As I understand, these are called Actual Arguments (num1, num2). These Actual Arguments are passed to the Formal Paramters, in this case x & y respectively. Is this correct?

    Also, when it gets passed, it like making another copy right?

  14. #14
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Also, could you please clarify what this means:

    Code:
    file type *variable name;
    What does the * mean?

    Some tell me that when you see that, it jsut meanst that the variable is a pointer.

    Others tell me * is "pointer to file type"

    So for example:

    Code:
    char *i_hate_pointers;
    The code above tells the compiler that i_hate_pointers is of type "pointer to char"


    Also, does the file type of a pointer for the value to which it is pointing to?

    Code:
    char *i_hate_pointers;
    *i_hate_pointers variable will point to a variable which has a char as the type in it. Is this correct?

  15. #15
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Are they the same? I like your way, its shorter.
    Function prototypes do not require the variable names, just their types.

    Also, when it gets passed, it like making another copy right?
    Correct. The values passed to the function are copies.

    Others tell me * is "pointer to file type"
    This is correct. The * means that the variable you are declaring is a pointer.

    The code above tells the compiler that i_hate_pointers is of type "pointer to char"
    correct.

    Here is a little example which may better illustrate:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int x;
    	int *px;
    
    	x = 5;
    	px = &x; // px is assigned the address of the variable x
    
    	printf("The value stored at address 0x%x is %d\n",px,*px);
    
    	return 0;
    }

Page 1 of 2 12 LastLast
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, 06: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, 09:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21