Thread: Getting a value from a function

  1. #1
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62

    Getting a value from a function

    I play with some C programs to learn C and I have a question.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void array_sum(int a[], int n);
    
    int main()
    {
    	int i,size;
    	int *p=(int *)malloc(sizeof(int) * size);
    	int buffer;
    		
    	
    	printf("\nHow many numbers you want to enter?\n");
    	scanf("%d", &size);
    	printf("\nEnter %d numbers:\n", size);
    	for(i = 0; i < size; i++)
    	{
    		scanf("%d", &buffer);
    		p[i] = buffer;
    	}
    	
    	array_sum(p, size);
    			
    	return 0;
    }
    
    void array_sum(int a[], int n)
    {
    	int i,sum;
    	sum=0;
    	
    	for(i = 0; i < n ; i++)
    	{
    		sum = sum + a[i];
    	}
    	printf("\nHere is the sum: %d\n", sum);
    }
    This program works fine, my question is how I can call the sum value from the out of the function. Such as I want to take out this code from the function and used under the main()

    Code:
    printf("\nHere is the sum: %d\n", sum);

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You can return it:
    Code:
    int array_sum(int a[], int n)
    {
    	int i,sum;
    	sum=0;
    	
    	for(i = 0; i < n ; i++)
    	{
    		sum = sum + a[i];
    	}
    
    	return sum;
    }
    Then you can do this in main:
    Code:
    printf("\nHere is the sum: %d\n", array_sum(p, size));

  3. #3
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Thanks for your reply, unfortunately I get these errors:

    Code:
    wk2ex.c: In function `main':
    wk2ex.c:31: error: invalid use of void expression
    wk2ex.c: In function `array_sum':
    wk2ex.c:45: warning: `return' with a value, in function returning void
    update:

    Sorry I didn't see void become int ;P . The program works fine. Thanks for your help
    Last edited by BoneXXX; 03-24-2007 at 10:28 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Does it?

    Because you allocate the space (using an uninitialised value for size) before you get to the prompt asking the user for the size.

    Plus, read the FAQ entry on casting malloc in C.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Sorry I didn't see void become int ;P . The program works fine. Thanks for your help
    But you still need to update the code to place your malloc after reading the size values. Now its just allocating memory with the junk present in size.

    ssharish2005

  6. #6
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    I see thanks for your helps.
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  7. #7
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    How about if there are different functions and one function returns something, how can I use that in other fuction?
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    From my experience there are two ways to do that. You can use globally declared variables or pointers.

    Globally declared variables will be recognized by all functions so you don't have to worry about returning values as the variable will be changed in each function.

    If you use pointers then you write a value to a certain memory location in a function and then can retrieve what was written there in another function.

  9. #9
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Okay thanks I will try to use "global" and "external" variables. Pointers confuses me ;P
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How about if there are different functions and one function returns something, how can I use that in other fuction?
    From my experience there are two ways to do that. You can use globally declared variables or pointers.
    The question is a little vague and the answer is probably somewhat misleading too. There probably is no reason to use globals (which are evil).

    Pass the return value of the first function to the second function as an argument. Call the first function from the second function, etc.

    Really depends on what you are doing.

  11. #11
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Okay I want to show I want to do:

    Code:
    int length(node_ptr list)
    {
    	node_ptr current = list->next;
    	int r = 0;
    	while(current)
    	{
    		r++;
    		current = current->next;
    	}
    	printf("\n&#37;d items were entered to the list", r);
    	return r;
    }
    This function returns me the value "r" and I want to use the "r" in this function. But I don't know what is the right way to do it?

    Code:
    void delete(int n, node_ptr list)
    {
    	node_ptr current = list->next;
    	int number;
    	printf("Enter a number to delete it from the list:");
    	scanf("\n%d",&number);
    	
    	while(current != NULL )
    	{
    		if(current->data_item == number)
    		{
    			free(current);
    			current = current -> next;
    			printf("The List is modified111111\n");
    			
    		}
    		else
    		{
    			current = current->next;
    		}
    	}
    	if(current == NULL)
    	{
    		printf("%d is not in the list22222\n", number);
    	}
    }
    the second function is not finished yet, i am working on it. I just wanted to show what I am asking.
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by BoneXXX View Post
    Okay I want to show I want to do:

    Code:
    int length(node_ptr list)
    {
    	node_ptr current = list->next;
    	int r = 0;
    	while(current)
    	{
    		r++;
    		current = current->next;
    	}
    	printf("\n%d items were entered to the list", r);
    	return r;
    }
    This function returns me the value "r" and I want to use the "r" in this function. But I don't know what is the right way to do it?

    Code:
    void delete(int n, node_ptr list)
    {
    	node_ptr current = list->next;
    	int number;
    	printf("Enter a number to delete it from the list:");
    	scanf("\n%d",&number);
    	
    	while(current != NULL )
    	{
    		if(current->data_item == number)
    		{
    			free(current);
    			current = current -> next;
    			printf("The List is modified111111\n");
    			
    		}
    		else
    		{
    			current = current->next;
    		}
    	}
    	if(current == NULL)
    	{
    		printf("%d is not in the list22222\n", number);
    	}
    }
    the second function is not finished yet, i am working on it. I just wanted to show what I am asking.
    The function that called length() would normally have a line of code like this:
    Code:
    number_items = length(list);
    so now number_items is assigned the return value r from length(). Like always with assignments, you need to be sure number_items is an appropriate data type to receive the value of r. As you see, the name can change (and usually should), when a value is returned from a function.

    Now in your call to the delete() function, just add the parameter number_items:
    Code:
    delete(n, list, number_items);
    All your function definitions and declarations need to be updated to reflect these changes, or the compiler will vomit in your general direction.

    Adak

  13. #13
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    In the delete function can I say

    Code:
    printf("the length was : &#37;d ", length (r))
    or do I have to do like you said? Thanks btw.
    Last edited by BoneXXX; 03-29-2007 at 02:36 AM.
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

  14. #14
    Brak BoneXXX's Avatar
    Join Date
    Mar 2007
    Location
    Bangkok
    Posts
    62
    Is there any other way than changing the parameter
    Code:
    delete(n, list, number_items);
    .
    Could some one give me an example of 'extern' please? I didn't understand it from online sources that I found.
    “Example isn't another way to teach, it is the only way to teach” Albert Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM