C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Basic Return Statement
  • Returning Multiple Values
  • Returning Strings

Was this helpful?

  1. Functions

Returning Data from Functions

In C programming, functions can return data to the calling code using the return statement. This allows functions to provide results or computed values for further use. Let's explore how to return data from functions in C.

Basic Return Statement

A function's return statement is used to send a value back to the calling code. Here's a simple example:

#include <stdio.h>

// Function definition with a return value
int square(int num) {
    return num * num;
}

int main() {
    // Function call and using the returned value
    int result = square(4);
    
    printf("Square: %d\n", result);

    return 0;
}

In this example, the square function calculates the square of a number and returns the result. The main function calls square(4), and the returned value (16) is assigned to the result variable.

Returning Multiple Values

C functions can return only one value directly. However, you can use pointers to return multiple values indirectly. Example:

#include <stdio.h>

// Function definition with multiple return values using pointers
void calculate(int a, int b, int *sum, int *product) {
    *sum = a + b;
    *product = a * b;
}

int main() {
    int resultSum, resultProduct;

    // Function call and using the returned values via pointers
    calculate(5, 3, &resultSum, &resultProduct);

    printf("Sum: %d\n", resultSum);
    printf("Product: %d\n", resultProduct);

    return 0;
}

In this example, the calculate function takes two numbers and calculates both their sum and product. The results are returned using pointers.

Returning Strings

Functions in C can also return strings, which are represented as arrays of characters. Example:

#include <stdio.h>

// Function definition returning a string
const char* greet() {
    return "Hello, world!";
}

int main() {
    // Function call and using the returned string
    const char *greeting = greet();

    printf("%s\n", greeting);

    return 0;
}

In this example, the greet function returns a string, and the main function prints the returned string.

Understanding how to return data from functions is crucial for building modular and reusable code in C programming.

If you have specific questions or if there are additional topics you'd like to explore, feel free to ask!

PreviousArguments and ParametersNextVariable Scoping

Was this helpful?