What do you mean by function return in C language?

CServer Side ProgrammingProgramming

C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

A function is a self-contained block that carries out a specific task.

The advantages of function in C language are as follows −

• Reusability

• The length program can be reduced.

• It is easy to locate and isolate a wrong function.

• It facilitates top-down modular programming.

Example

Following is the C program for functions −

#include<stdio.h>
/*Function prototypes*/
myfunc();
main(){
myfunc();
}
/*Function Defination*/
myfunc(){
printf("Hello ");
}

Here,

• In calculations, we generally expect a function to return a value. But, it may or may not accept the arguments.

• This return value has a type int, float, char or anything else.

• Default type of function is integer.

Example

Another program for the function is as follows −

int total (){
int a,b,c;
a=10;
b=20;
c=a+b;
return c; //it returns the c value i.e. prints the result
}

Output

When the above program is executed, it produces the following result −

30

c=a+b;
return c;

We can replace with single-step like return a+b;

If you forget to return a value in a function, it returns the warning message in most of the C compilers. This message warns that you must return a value. Warnings may not stop the program execution but, errors stop it.

Example Program

Given below is the C program for return function −

int main(){
---
---
---
return 0;
}

The default return value for an integer type is 0.

If you do not insert return 0 or any other value in your main() function a, 0 will be returned automatically.

If you want to return an int value in your function, it is preferred to mention the return value in your function header.

Returning from a Function

A function returns a single value by means of the return statement.

If the changes are made within the function to the variables, then they are local to that function. A calling function's variables are not affected by the actions of a called function.

The calling function chooses to ignore the value returned by the called function. For example, printf and scanf return values are usually ignored.

The value returned by a function is used in a more complex expression, or it may be assigned to a variable.

Updated on 08-Mar-2021 05:56:30