Computer Science

How to return multiple values from a function in C

In C, a function can only return one value. However, you can use pointers to return multiple values from a function. By passing pointers as arguments to the function, you can modify the values of the variables pointed to by the pointers and return multiple values.

Written by Perlego with AI-assistance

3 Key excerpts on "How to return multiple values from a function in C"

  • Book cover image for: C All-in-One Desk Reference For Dummies
    • Dan Gookin(Author)
    • 2011(Publication Date)
    • For Dummies
      (Publisher)
    pointers .
    Pointers are covered in Book IV. They’re a heavy duty part of the C language that is at the same time mysterious and confusing, while being very powerful. Fortunately they’re explained rather well in Book IV, so I leave you to understand that definition before you begin to use pointers to send arrays and strings to functions.
    Sending strings off to functions is tricky stuff. It encroaches on a C language topic called pointers, which everyone agrees is kind of scary (but primarily because it’s never properly explained).
    Besides: I have way too much information in this chapter already!

    Functions That Return a Value

    I would guess that the majority of functions return a value. Some may eat a value as well, but most typically send some value back — a valuable value to value. Or I could say a valuable variable value to value.

    Returning a value with the return keyword

    To return a value, a function must obey two rules:
    Warning! Rules approaching.
    FThe function has to be defined as a certain type (int , char , float , etc. — just like a variable). Something other than void , because void functions (by their definition) don’t return values.
    FThe function has to return a value.
    The function type tells you what type of value it returns. For example:
    double nationalDebt(void)
    The nationalDebt() function returns the national debt of the United States as a double value. No input is required. Even if input were offered, Congress would most likely ignore it.
    int birthday(int date);
    The function birthday() is defined. It’s an integer function that returns an integer value. (It also requires an integer parameter, date
  • Book cover image for: C Programming
    No longer available |Learn more

    C Programming

    A Self-Teaching Introduction

    The return statement returns only one value at a time. A void returning function can use the return statement with no value as: void example( ) { printf(”\n Thanks to God”); return; } 222 • C PROGRAMMING Also note that the return statement can appear anywhere in the function body. It means that the programmer is free to use it before the closing brace of the function body. For example, #include void main( ) { void test( ); test( ); } void test( ) { int u; printf(“Enter your number: “); scanf(%d”, &u); if ( u < 0) { printf(“\n It is a negative number”); return; } else { printf(”\n It is a positive number”); return; } } OUTPUT (after running): Enter your number: -80 It is a negative number If you call a value-returning function and use it in a context in which no value is expected, say, you are not assigning the return value to any- thing, actually nothing unusual happens. There is no warning message by the compiler. The function executes correctly and it returns its value. The caller function can use it or ignore it. IV. Functions with Arguments and a Return Value Those functions that receive some arguments from the calling function and return some value are put under the next category. Here we use the call-by-value method, which is discussed in Section 3.4. NOTE ARRAYS AND POINTERS • 223 V. Recursions Recursion is defined as a process in which a function calls itself again and again. It is a technique of defining a problem in terms of one or more smaller versions of the same problem. The solution to the problem is built on the results from the smaller versions. A recursive function is one which calls itself directly or indirectly to solve a smaller version of its task until a final call which does not require a self-call. For example, Add( ) { Add ( ); } This means that the function add( ) calls itself. Advantages of Recursion 1. Recursion makes the program compact.
  • Book cover image for: Mastering C Pointers
    eBook - PDF

    Mastering C Pointers

    Tools for Programming Power

    • Robert J. Traister(Author)
    • 2014(Publication Date)
    • Academic Press
      (Publisher)
    Chapter 8 Pointers and Functions While pointers are useful, powerful, and indispensable throughout all phases of C language programming, their capabilities yield high-powered program performance especially when dealing with programmed functions. We already know that arguments are passed to and from func-tions by value. It is not possible to change the value of a calling program variable within a function . . . unless we have the address of a variable in the calling program. C language functions can return values that can be, in turn, assigned to variables within the calling program, but through pointer operations, we can write specialized functions that will return a value to the calling program and change the values in other variables whose memory addresses are handed to the func-tion. In this manner, it is possible to write a function that, figur-atively, returns many values in a single operation. Again, the direct programming of pointers is what gives C lan-guage its power, expressiveness, and authority. It is also the pointer that has vaulted C language to the top of the list of languages most desirable for software development. The following program incorporates a special function to return 123 124 Chapter 8 the square of a value passed to it. This is a useless function for any but tutorial purposes, but as the discussion continues other exam-ples will be provided that will provide some practical application tools. The program follows: ma i n( ) { i nt x 7 y ; x = 1 2 ; y - 7 ; x = square(x); y = square(y); printf(%d %dn n , x, y); } i nt square(a) int a ; i return(a * a); } The squareO function is passed a value from the calling program. This value is assigned to a , a variable that is internal to the func-tion. The function return value is a * a , or the square of the passed value. This program will display the values: 144 49 on the monitor screen.
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.