Function Return Values and Prototypes In C

Let’s look at ways to write function return values so as to increase our program’s effectiveness.

Return values enable us to pass data back from a receiving function to its calling function. When we want to return a value from a function to its calling function we put the return value after the return statement.

Note that we must include a function prototype in our program. In C, a function prototype models the function so as to inform the compiler of each function’s parameter list and return type. Prototyping a function is easy; all we need to do is copy the function’s header to the top of our program. We ought to recall here that the function header contains the return type, the function name, and the type of each argument sent to the function.

#include <stdio.h>

double average(int nums[], int arraySize);

int main(void){

    int nums[3];

    int avg, i;

    for(i = 0; i < 3; i++){
        printf("Please enter a number: ");
        scanf(" %d", &nums[i]);
        printf("%d number entered.\n", i + 1);
    }

    printf("The average is: %f\n", average(nums, 4));

    return 0;

}


//function returns a double value
double average(int nums[], int arraySize){
    int total;
    double count = arraySize;
    while(arraySize >= 0){
        total += nums[arraySize--];    
    }
                        
    return total / count;
}

Note that the prototypes for library functions are contained in header files, such as stdio.h and string.h.

We must always precede a function name with its return data type. Because the variable returned from the average() function above is a double, the double return type was placed before the function’s name when we defined the function. Note that we can only return a single value to a calling function.

We can place a call to a function on the right side of an assignment operator since the program replaces a function call with its return value when the return takes place. In a sense, a function that returns a value becomes that value.

#include <stdio.h>

int SquareNum(int num);

int main(void){
    
    int num;
    
    int square;
    
    printf("What number do you want squared?\n");
    scanf(" %d", &num);
    
    //assign return value to
    //the int variable square
    square = SquareNum(num);
    
    printf("%d squared is %d\n", num, square);
    
    
    return 0;
    
}

int SquareNum(int num){
    return num * num;
}

It is possible to nest function calls within other function calls.

#include <stdio.h>

//function prototype
double linearFunction(int rise, int run, int x, int b);

int main(void){
    
    int rise, run, intercept;
    
    printf("Enter the rise: ");
    scanf(" %d", &rise);
    
    printf("Enter the run: ");
    scanf(" %d", &run);
    
    printf("Enter the y-intercept: ");
    scanf(" %d", &intercept);
    
    for(int i = 0; i < 10; i++){
        printf("f(%d) = ", i);
        printf("%f\n", linearFunction(rise, run, i, intercept));
    }
    
    
    return 0;

}

double linearFunction(int rise, int run, int x, int b){
    return ((double)rise/run)*x+b;
}

Note that sometimes it is clearer to call the function and assign its return value to a variable before using it.

Our next program finds the digit sum of a number given by the user. The digit sum is all of the whole numbers from one to a certain number added together. We will also call a user-defined function from within another user-defined function for the first time.

#include <stdio.h>

int DigitSum(int num);
void printNumber(int i, int num);

int main(void){
    int num;
    printf("Enter a number: ");
    scanf(" %d", &num);
    
    printf("The digit sum of %d is %d\n", num, DigitSum(num));

}

int DigitSum(int num){

    int i;
    int sum;

    if(num <= 0){
        return num;
    } else {
        for(i=1, sum=0;i<=num;i++){
            //call another user defined function
            printNumber(i, num);
            sum+=i;
        }
    }

    return sum;
} //end DigitSum()

//prints either number +
//or else just the number
void printNumber(int i, int num){
    if(i<num){
        printf("%d + ", i);
    } else {
        printf("%d\n", i);
    }
}// end printNumber()

Note that when we prototype a function that does not return a value we declare it to be of type void.

If you’d like to learn more about C, take a look at my book that contains 300 new incremental lessons on C:

http://www.amazon.com/Big-Als-C-Standard-ebook/dp/B00A4JGE0M

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s