Pre-requisite: Functions in C
C return statement ends the execution of a function and returns the control to the function from where it was called. The return statement may or may not return a value depending upon the return type of the function. For example, int returns an integer value, void returns nothing, etc.
In C, we can only return a single value from the function using the return statement and we have to declare the data_type of the return value in the function definition/declaration.
Syntax:
return return_value;
Working of Return Statement
There are various ways to use return statements. A few are mentioned below:
1. Methods not returning a value
In C, one cannot skip the return statement when the return type of the function is non-void type. The return statement can be skipped only for void types.
A. Not using a return statement in void return type function:
While using the void function, it is not necessary to use return as the void itself means nothing(an empty value).
Syntax:
void func()
{
.
.
.
}
Example:
C
// C code to show not using return
// statement in void return type function
#include <stdio.h>
// void method without return statement
void Print()
{
printf("Welcome to GeeksforGeeks");
}
// Driver method
int main()
{
// Calling print
Print();
return 0;
}
Output:Welcome to GeeksforGeeks
B. Using the return statement in the void return type function
As void means empty, we don't need to return anything, but we can use the return statement inside void functions as shown below. Although, we still cannot return any value.
Syntax:
void func()
{
return;
}
This syntax is used in function as a jump statement in order to break the flow of the function and jump out of it. One can think of it as an alternative to "break statement" to use in functions.
Example:
C
// C code to show using return
// statement in void return type function
#include <stdio.h>
// void method with return statement
void Print()
{
printf("Welcome to GeeksforGeeks");
// void method using the return statement
return;
}
// Driver method
int main()
{
// Calling print
Print();
return 0;
}
Output:Welcome to GeeksforGeeks
But if the return statement tries to return a value in a void return type function, that will lead to errors.
Incorrect Syntax:
void func()
{
return value;
}
Example:
C
// C code to show using return statement
// with a value in void return type function
#include <stdio.h>
// void method
void Print()
{
printf("Welcome to GeeksforGeeks");
// void method using the return
// statement to return a value
return 10;
}
// Driver method
int main()
{
// Calling print
Print();
return 0;
}
Warnings:prog.c: In function 'Print':
prog.c:12:9: warning: 'return' with a value, in function returning void
return 10;
^
2. Methods returning a value
For functions that define a non-void return type in the definition and declaration, the return statement must be immediately followed by the return value of that specified return type.
Syntax:
return-type func()
{
return value;
}
Example:
C
// C code to illustrate Methods returning
// a value using return statement
#include <stdio.h>
// non-void return type
// function to calculate sum
int SUM(int a, int b)
{
int s1 = a + b;
// method using the return
// statement to return a value
return s1;
}
// Driver method
int main()
{
int num1 = 10;
int num2 = 10;
int sum_of = SUM(num1, num2);
printf("The sum is %d", sum_of);
return 0;
}
Note: A function can only return a single value using return statement. To return multiple values, we use pointers or structures. Know more about refer to the article - How to return multiple values from a function in C or C++?.
Similar Reads
Jump Statements in C
In C, jump statements are used to jump from one part of the code to another altering the normal flow of the program. They are used to transfer the program control to somewhere else in the program. In this article, we will discuss the jump statements in C and how to use them. Types of Jump Statements
5 min read
C if else Statement
The if else in C is an extension of the if statement which not only allows the program to execute one block of code if a condition is true, but also a different block if the condition is false. This enables making decisions with two possible outcomes.Example:C#include <stdio.h> int main() { in
3 min read
_Noreturn function specifier in C
In C, the _Noreturn specifier is used to indicate that a function does not return a value. It tells the compiler that the function will either exit the program or enter an infinite loop, so it will never return control to the calling function. This helps the compiler to optimize code and issue warni
2 min read
Implicit Return Type int in C
In C, every function has a return type that indicates the type of value it will return, and it is defined at the time of function declaration or definition. But in C language, it is possible to define functions without mentioning the return type and by default, int is implicitly assumed that the ret
2 min read
remove() in C
In C, remove() is a standard library function is used to delete a file from the file system. It provides a simple way to manage files in the storage from a C program.Example:C#include <stdio.h> int main() { // Remove the file remove("gfg.txt") return 0; }OutputFile deleted successfully.Explana
3 min read
C Cheat Sheet
This C Cheat Sheet provides an overview of both basic and advanced concepts of the C language. Whether you're a beginner or an experienced programmer, this cheat sheet will help you revise and quickly go through the core principles of the C language. In this Cheat Sheet, we will delve into the basic
15+ min read
system() in C/C++
The system() function is used to invoke an operating system command from a C/C++ program. For example, we can call system("dir") on Windows and system("ls") in a Unix-like environment to list the contents of a directory.It is a standard library function defined in <stdlib.h> header in C and
6 min read
What does main() return in C and C++?
C According to coding standards, a good return program must exit the main function with 0. Although we are using void main() in C, In which we have not suppose to write any kind of return statement but that doesn't mean that C code doesn't require 0 as exit code. Let's see one example to clear our t
3 min read
scanf in C
In C, scanf() is a function is used to read data from stdin (standard input stream i.e. usually keyboard) and stores the result into the given arguments. It is defined in the <stdio.h> header file.Example:C#include <stdio.h> int main() { int n; // Reading an integer input scanf("%d",
3 min read
Sequence Points in C | Set 1
In this post, we will try to cover many ambiguous questions like following. Guess the output of following programs. C // PROGRAM 1 #include <stdio.h> int f1() { printf ("Geeks"); return 1;} int f2() { printf ("forGeeks"); return 1;} int main() { int p = f1() + f2(); return
4 min read