return Statement in C Language with Examples

What is return Statement in C?

In C programming language, the return statement is used to terminate the execution of a function and transfer program control back to the calling function. It can also return a value (optional) to the calling function. The value returned by a function should be compatible with the return type declared in the function definition. For example, an int function generally returns an integer value, while a float function returns a floating-point value.

C प्रोग्रामिंग भाषा में return statement का उपयोग किसी function के execution को समाप्त करके program control को calling function पर वापस भेजने के लिए किया जाता है। इसके द्वारा calling function को एक value भी return की जा सकती है, जो वैकल्पिक होती है। Function द्वारा return की जाने वाली value, function definition में declare किए गए return type के अनुसार होनी चाहिए। उदाहरण के लिए, int function सामान्यतः integer value return करता है, जबकि float function floating-point value return करता है।

A function declared with the void return type does not return a value. It can use return; to terminate its execution early, or it can simply reach the end of its function body.

जिस function का return type void होता है, वह कोई value return नहीं करता है। ऐसे function में execution को जल्दी समाप्त करने के लिए return; का उपयोग किया जा सकता है या function अपने अंतिम statement तक execute होकर समाप्त हो सकता है।

Syntax of return Statement

1. Return statement with an expression:

return expression;

इसका उपयोग किसी value को calling function पर वापस भेजने के लिए किया जाता है।

2. Return statement without an expression:

return;

इसका उपयोग void function के execution को समाप्त करने के लिए किया जाता है।

Example 1: Simple return Statement in C

Program to demonstrate the use of return statement.

#include <stdio.h>
int main(void)
{
printf("Welcome to C Programming");
return 0;
}

Output:

Welcome to C Programming

Explanation: The main() function executes the printf() statement and displays the message. The statement return 0; terminates the main() function and returns 0 to the operating system, conventionally indicating successful program execution.

व्याख्या: main() function में printf() statement execute होकर message display करता है। इसके बाद return 0; statement main() function को समाप्त करके operating system को 0 return करता है, जो सामान्यतः successful program execution को दर्शाता है।

Example 2: Returning a Value from a Function

Program to calculate the sum of two numbers using a function.

#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
int main(void)
{
int result;
result = sum(10, 20);
printf("Sum = %d", result);
return 0;
}

Output:

Sum = 30

Explanation: The sum() function receives two integer arguments, adds them, and returns the result using return a + b;. The calling function main() receives the returned value and stores it in the variable result.

व्याख्या: sum() function दो integer arguments प्राप्त करता है, उन्हें जोड़ता है और return a + b; के माध्यम से परिणाम वापस भेजता है। Calling function main() इस returned value को प्राप्त करके result variable में store करता है।

Example 3: Function Returning Maximum of Two Numbers

Program to find the greater number using a function and return statement.

#include <stdio.h>
int maximum(int a, int b)
{
if(a > b)
return a;
else
return b;
}
int main(void)
{
int result;
result = maximum(25, 40);
printf("Maximum Number = %d", result);
return 0;
}

Output:

Maximum Number = 40

Explanation: The maximum() function compares two numbers. If a is greater than b, it returns a; otherwise, it returns b. The returned value is displayed by the main() function.

व्याख्या: maximum() function दो संख्याओं की तुलना करता है। यदि a, b से बड़ा होता है, तो यह a return करता है; अन्यथा b return करता है। Returned value को main() function द्वारा display किया जाता है।

Example 4: return Statement in void Function

Program to demonstrate return statement without returning a value.

#include <stdio.h>
void display(int n)
{
if(n < 0)
{
printf("Negative number is not allowed.");
return;
}
printf("Number = %d", n);
}
int main(void)
{
display(-5);
return 0;
}

Output:

Negative number is not allowed.

Explanation: The display() function has a void return type, so it does not return any value. When the value of n is negative, return; immediately terminates the function and transfers control back to main().

व्याख्या: display() function का return type void है, इसलिए यह कोई value return नहीं करता है। जब n का मान negative होता है, तब return; function को तुरंत समाप्त करके control को वापस main() function पर भेज देता है।

Important Points About return Statement

  1. The return statement is used to terminate a function.

  2. It transfers program control back to the calling function.

  3. It can return a value to the calling function.

  4. The returned expression must be compatible with the function's return type.

  5. A void function does not return a value.

  6. A void function can use return; without an expression.

  7. A non-void function should return an appropriate value on every execution path that reaches the end of the function. Reaching the end without returning a value is undefined behavior when the returned value is used.

  8. The return 0; statement in main() conventionally indicates successful program execution.

  9. return statement का उपयोग किसी function को समाप्त करने के लिए किया जाता है।

  10. यह program control को वापस calling function पर भेजता है।

  11. इसके द्वारा calling function को एक value return की जा सकती है।

  12. Return की गई expression, function के return type के अनुसार compatible होनी चाहिए।

  13. void function कोई value return नहीं करता है।

  14. void function में बिना expression के return; का उपयोग किया जा सकता है।

  15. Non-void function के प्रत्येक ऐसे execution path में उचित value return होनी चाहिए जो function के अंत तक पहुँचता है। यदि function value return किए बिना समाप्त होता है और उस returned value का उपयोग किया जाता है, तो undefined behavior होता है।

  16. main() में return 0; सामान्यतः successful program execution को दर्शाता है।

Difference Between break and return Statement

break Statementreturn Statement
It terminates the innermost enclosing loop or a switch statement.It terminates the execution of the current function.
Control moves outside the loop or switch.Control returns to the calling function.
It does not return a value.It can return a value to the calling function.
It is used inside loops and switch statements.It is used inside functions.

break Statement
return Statement
यह सबसे अंदर वाले संबंधित loop या switch statement को समाप्त करता है।यह वर्तमान function के execution को समाप्त करता है।
Control loop या switch के बाहर चला जाता है।Control वापस calling function पर चला जाता है।
यह कोई value return नहीं करता है।यह calling function को value return कर सकता है।
इसका उपयोग loops और switch statements के अंदर किया जाता है।इसका उपयोग functions के अंदर किया जाता है।

The return statement is an essential part of functions in C programming. It is used to terminate function execution and optionally send a value back to the calling function. Understanding the return statement helps programmers create reusable, modular, and well-structured programs.

C programming में return statement functions का एक महत्वपूर्ण भाग है। इसका उपयोग function के execution को समाप्त करने और आवश्यकता होने पर calling function को value वापस भेजने के लिए किया जाता है। return statement को समझने से reusable, modular और अच्छी तरह से structured programs बनाने में सहायता मिलती है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025