C Return Types 🎯

beginner
18 min

C Return Types 🎯

Welcome to our deep dive into C Return Types! In this lesson, we'll explore the fascinating world of functions and how they communicate their results back to the calling environment. Let's embark on this journey together!

What are Return Types? 📝

Return types are a crucial part of function declarations in C programming. They define the type of data a function will return upon its completion. This allows the caller to know what kind of value to expect from the function.

Common C Return Types 💡

  1. int: Returns an integer value.
  2. float: Returns a floating-point number.
  3. char: Returns a character value.
  4. void: Returns no value (we'll discuss this later).

Creating a Simple Function ✅

Let's create a function that returns an integer value.

c
int addNumbers(int a, int b) { int sum = a + b; return sum; }

Here, we have a function called addNumbers that takes two integer arguments and returns their sum.

Function Calling 📝

Now, let's call this function and use the result in our program.

c
#include <stdio.h> int addNumbers(int a, int b); int main() { int result = addNumbers(5, 3); printf("The sum is: %d\n", result); return 0; }

In the main function, we call addNumbers with two integers, 5 and 3, and store the returned value in the result variable. Then, we print the result to the console.

The void Return Type 📝

A function with void as its return type doesn't return any value. These functions are typically used for side effects, like modifying variables within the calling scope.

c
void greetUser(char* name) { printf("Hello, %s!\n", name); }

This greetUser function takes a character pointer as an argument and greets the user using their name. Notice that it doesn't return any value since we don't need one.

Quiz Time! 💡

Quick Quiz
Question 1 of 1

What does the `void` return type indicate in a C function?

Wrapping Up ✅

Now that you've grasped the concept of return types, you're well on your way to mastering C programming! In the next lesson, we'll delve deeper into functions and explore how to handle different types of data. Until then, happy coding! 🚀