Thread: return value in C function

  1. #1
    Registered User
    Join Date
    Apr 2020

    return value in C function

    Hello all,

    As we know you can't return arrays from functions in programming language C. I am working on an open source code base written in C. While working on one of its function, I wanted to be sure if I have correctly understood the return value of the following function. Can someone please help me understand what exactly below function bucket_straw2_choose is returning?

    static int bucket_choose (const struct crush_bucket_straw2 *bucket, int x, int r, 
                              const struct crush_choose_arg *arg, int position) {
         __u32 *weights = get_choose_arg_weights(bucket, arg, position);
         __s32 *ids     = get_choose_arg_ids(bucket, arg);
    for (i = 0; i < bucket->h.size; i++) {
    dprintk("weight 0x%x item %d\n", weights[i], ids[i]);
    if (weights[i]) {
           draw = generate_exponential_distribution(bucket->h.hash,  x, ids[i],  r,  
            } else {
                         draw = S64_MIN;
            if (i == 0 || draw > high_draw) {
                high = i;
                high_draw = draw;
        return bucket->h.items[high];
    struct crush_bucket {
        __s32  id;        
        __u16  type;      
        __u8   alg;        
        __u8   hash;       
        __u32  weight;    
        __u32  size;    
        __s32  *items;    
    struct crush_bucket_straw2 {
           struct crush_bucket h; 
        __u32 *item_weights;   

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    bucket_straw2_choose does not appear in the code. Did you mean the bucket_choose function? That one is declared with a return type of int, so it returns an int. If you're confused by the static keyword, in this context that declares the function name as having internal linkage, i.e., in this source file (or more precisely, translation unit, i.e., source file + included headers, but that distinction isn't very important here), the name bucket_choose refers to this function, but in another source file it would refer to something else.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Return pointer to a vector of objects as a function return
    By Adaptron in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2016, 09:23 AM
  2. Replies: 4
    Last Post: 06-10-2013, 04:03 PM
  3. Function return a value
    By gr8npwrfl in forum C Programming
    Replies: 16
    Last Post: 07-16-2008, 10:10 PM
  4. return function
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 05-20-2007, 09:12 AM
  5. Replies: 6
    Last Post: 04-09-2006, 04:32 PM

Tags for this Thread