Return c что это
Функция может иметь результат — некоторое значение, которое оно возвращает во внешний код. Для возвращения результата функция применяет оператор return , которые имеет следующие способы использования:
Первая форма может использоваться в тех функциях, которые не возвращают никакого значения, то есть имеют в качестве возвращаемого типа void .
Вторая форма применяется, если функция имеет в качестве возвращаемого типа любой тип, кроме void . Выражение после оператора return представляет возвращаемое значение.
В прошлой теме была определена функция sum , которая в качестве возвращаемого типа имела тип void , то есть по факту ничего не возвращает:
Хотя эта функция не использует явно оператор return , но компилятор сам его добавляет автоматически в конец функции:
Эти оба определения фактически равнозначны.
Но также функция может возвращать какое-то конкретное значение. Например, изменим функцию sum, чтобы она возвращала сумму чисел:
Теперь функция sum в качестве возвращаемого типа имеет тип int . Если тип функции отличен от void , то функция обязательно должна возвращать значение данного типа. Для возвращения значения применяется оператор return , после которого указывается возвращаемое значение.
В данном случае функция sum возвращает сумму параметров:
Если функция возвращает какое-либо значение, то при ее вызове мы можем получить это значение в переменную:
Прототип функции sum будет выглядеть так:
Логика возвращения результата может быть более сложной. Например:
В данном случае функция calculate() принимает три параметра. Первые два параметра представляют числа, над которыми выполняется арифметическая операция. А третий параметр представляет знак операции. И в зависимости от знака операции функция возвращает либо сумму, либо разность чисел. Или число 0, если операция не поддерживается.
Return Statement 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:

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).
The return statement in C
The return statement is used to return some value or simply pass the control to the calling function. The return statement can be used in the following two ways.
- return;
- return expression;
The first form of the return statement is used to terminate the function and pass the control to the calling function. No value from the called function is returned when this form of the return statement is used.
The following program demonstrates the use of the first form of the return statement.
Expected Output:
How it works
Let's say the user entered 17 (value of variable n ), then eligible_or_not() function is called, this function expects one argument of type int , which we have correctly supplied as n . The value of variable n is assigned to variable age . Remember age is a local variable, and thus only available inside eligible_or_not() function. if condition (age >= 18) is tested, since it is false, the statement under if block is omitted. Control passes to else statement and condition (age == 17) is tested, since it is true, statements under else if block is executed. The return statement under the else if block passes the control back to calling function i.e main() . If the return statement would not have been there in the else if block, the control would have been moved ahead to execute the statement following if-else statement.
The second form of the return statement is used to return values from a function. The expression following return can be any constant, variable, function call etc.
The following program computes the factorial of a number using a function.
Expected Output:
How it works
There is nothing extraordinary happening in the main() function, so we are only going to explain how factorial() function works. Let’s say the user entered 5, in line 18 the factorial() function is called, along with an argument of type int. In line 30, if condition (n==0) is checked, since it is false, statements inside the if block are omitted. If the condition (n==0) would have been true, then the statement return 1; (Why we are returning 1 , because factorial of 0 is 1 ) would have been executed, and the control would have been transferred back to main() function, effectively terminating the factorial() function.
In line 34, two variables f and i of type int are declared and the variable f is assigned a value of 1 .
In line 36, for loop's initialization expression is executed, i is assigned the value of n . Then the condition (i>0) is tested, since it is true, statement inside the for body is executed. At this point, the value of f is 5 . This completes the first iteration. Then update expression i— is executed. This process continues until i is greater than 0 . The following table shows the value of i and fact after each iteration.
| Iteration | Value of i | Value of fact |
|---|---|---|
| 1 | 5 | 5 |
| 2 | 4 | 5*4 = 20 |
| 3 | 3 | 20* 3 = 60 |
When i reaches 0 , the condition (i > 0) becomes false and the control breaks out of the for loop to execute statements following the loop.
In line 40, the return statement causes the factorial() function to terminate and return the value of variable f to the calling function main() .

report this ad
Return Statement in C Language
Return Statement in C Language with Examples
In this article, I am going to discuss Return Statement in C Language with examples. Please read our previous articles, where we discussed Continue Statement in C. At the end of this article, you will understand what is Return Statement in C and when and how to use the Return statement in C with examples.
Return Statement in C Language:
The return statement terminates the execution of a function and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. A return statement can also return a value to the calling function. A return statement causes your function to exit and hand back a value to its caller. The point of functions, in general, is to take in inputs and return something. The return statement is used when a function is ready to return a value to its caller.
How Does the Return Statement Work in C language?

Syntax: return (expression);
Program to Understand Return Statement in C Language:
Output:
In the next article, I am going to discuss the Goto Statement in C Language with Examples. Here, in this article, I try to explain Return Statement in C Language with Examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.