Return Statement in C

Return is a Keyword. It used to return the some value in function. Return is not mandatory. It's up to you whether you want to return value or not. It can be used in two ways.

A function may return a value or may not. It depend on function data type. Whether you are using int, float, char, double or void. Here void data type return nothing and int, float, char, double data type return 0 or 1.

Learn more about function here

Two ways are there

Syntax of return statement:

    
    return_type function_name(parameter optional);
    {
    
    body of statement;
    
    return ;
    }
    

Why we use return?

To return the some value we use return statement. Return is not mandatory. It's up to you whether you want to return value or not.

If you don't write any return type before main function then by default main function is int.

main Function

main function always return null value with void data type. If you don't use void data type before the main function then by default it will return int. using main function without any data type it will give error, return type is required.

Example of main function return type:

	
    main()    //no void, then by default it return int 
    {
    	return 0;
    }

Example of return type program:

	
    int main()
    {
    	printf("Line 1\n");
        f1();   //function caller
        printf("F1 after");
    }
    
    void f1()    //function declaration
    {
    	printf("Return f1 function.");
        
    }
    

Output:

	
    Line 1
    Return f1 function.
    F1 after
    

Post Your Comment