Return and Void Statement of Function

In this article, you will learn about return and void statements of function as well as local, global and static variables and recursion as well.
 
 
Return and Void Statements of Function in C Programming.

 

Return Statement

The return statement is used in a function to return a value to the calling function it may also be used for an immediate exit from the called function to the calling function without returning a value. This statement can appear anywhere inside the body of the function.
 
For example:
 
 
return 1;
return x++;
return (3*sum(a,b));
 
    // program to find sum of n integer numbers using function in C programming
    #include <stdio.h>
    #include <conio.h>
    int sum(int n);
    void main()
    {
        int n, x;
        printf("Enter the number:n");
        scanf("%d", &n);
        x = sum(n);
        printf("nThe sum of %d number is =%d", n, x);
        getch();
    }
    int sum(int n)
    {
        int i, num, s = 0;
        for (i = 1; i <= n; i++)
        {
            printf("Enter the number: ");
            scanf("%d", &num);
            s = s + num;
        }
        return s;
    }
 

void Statement

The void statement is used in the function when there is no need to return the value. It means this function does not give output to the called function. Though the function is return type void it may contain a return keyword to transfer the control at end of the function.
 
Syntax:
 
void function_name(list_of_parameters);
 

Types of user-defined function

The user-defined function can be classified into four categories on the basis of the arguments and return value.
  1. Function with no arguments and no return value.
  2. Function with no argument and a return value.
  3. Function with argument and no return value.
  4. Function with argument and a return value.
 

a. Function with no argument and no return value

This type of function will not return any value to the calling function so we use return type void and it will not also pass value to the called function on the argument whether kept empty or use void. The syntax is as:
 
 
    void func (void); //return type is void and argument also void
    main()
    {
    ………
    func(); //function call
    ……..
    }
    void func(void) //function defination
    {
    ………
    statement;
    ………
    }
 
 
    // program of user-defined function with no argument and no return value in C programming
    #include <stdio.h>
    #include <conio.h>
    void sum(void);
    void main()
    {
        sum();
        getch();
    }
    void sum(void)
    {
        int sum, a, b;
        printf("Enter first number:");
        scanf("%d", &a);
        printf("Enter second number:");
        scanf("%d", &b);
        sum = a + b;
        printf("The sum of two number is %d", sum);
    }
 

b. Function with no argument and a return value

This type of function does not receive any arguments but can return a value. Here it will not pass the argument but it can return value. So we used int as the return type it will return an integer value. The syntax is as:
 
 
    int func(void); //function declaration with no argument but return type
    main()
    {
    int r;
    ………
    r=func(); //calling function
    …….
    }
    int func(void)
    {
    …..
    ……..
    return (expression);
    }
 
 
    // program of user-defined function with no argument and return value in C programming
    #include <stdio.h>
    #include <conio.h>
    int sum(void);
    void main()
    {
        printf("The sum of two number is %d", sum());
        getch();
    }
    int sum(void)
    {
        int sum, a, b;
        printf("Enter first number:");
        scanf("%d", &a);
        printf("Enter second number:");
        scanf("%d", &b);
        sum = a + b;
        return sum;
    }
 

c. Function with argument and no return value

This type of function can pass the argument but will not return value to the main() function. Here we use void as the return type and value can be pass from the main() function to the called function. The syntax is as:
 
 
    void func(int,int); //function declaration with no return value but argument
    main()
    {
    ……
    func (a,b); //calling function
    …….
    }
    void func (int c, int d) //function definition
    {
    …….
    statemnts
    ……….
    }
 
 
    // program of user defined function with argument and no return value in C programming
    #include <stdio.h>
    #include <conio.h>
    void sum(int a, int b);
    void main()
    {
        int a, b;
        printf("Enter first number: ");
        scanf("%d", &a);
        printf("Enter second number: ");
        scanf("%d", &b);
        getch();
    }
    void sum(int a, int b)
    {
        int s;
        s = a + b;
        printf("The sum of two number is %d", s);
    }
 

d. Function with argument and a return value

This type of function will pass the value from the main program to the function and the calculated value will be a return to the main function. So we use the int as the return type and pass the argument. The syntax is as:
 
 
    int func(int, int); //function declaration with return type and argument
    main()
    {
    int r;
    ……
    r=func(a,b); //calling function
    …..
    func (c,d);
    }
    int func (int a, int b) //function definition
    {
    ……..
    …….
    return (expression);
    }
 
 
    // program of user defined function with argument and return value in C programming
    #include <stdio.h>
    #include <conio.h>
    int sum(int a, int b);
    void main()
    {
        int a, b, s;
        printf("Enter first number: ");
        scanf("%d", &a);
        printf("Enter second number: ");
        scanf("%d", &b);
        s = sum(a, b);
        printf("The sum of two number is %d", s);
        getch();
    }
    int sum(int a, int b)
    {
        int x;
        x = a + b;
        return x;
    }
 

Local, global, and static variables

Local variables

The variables that are defined within the body of a function or a block are local to that function or block only and are called local variables. For example:
 
    void func();
    {
    int a,b,c;
    ………
    ……..
    }
 
 
 
The local variables can be used only in those functions or blocks, in which they are declared. The same name variables may be used in different functions.
 

Global Variables

The variables that are defined outside any function are called global variables. All functions in the program can access and modify global variables. It is useful to declare a variable global if it is to be used by many functions in the program. Global variables are automatically initialized to 0 at the time of declaration.
 
    int a=10, b=20; //declaration of global variable before main() function
    void main()
    {
    Local variable;
    ………..
    }
 
 
 

Static variables

Static variables are declared by writing keyword static in from of the declaration.
 
Syntax:
static data_type var_name;
 
 
A static variable is initialized only once and the value of a static variable is retained between function calls. If static variables are not initialized then it is automatically initialized to 0.
 
 

Recursion

Recursion is a powerful technique for writing complicated problems in an easy way. According to this technique, a problem is defined in terms of itself. The problem is solved by diving it into smaller problems, which are similar in nature to the original problem. These smaller problems are solved and their solutions are applied to get the final solution of our original problem. Therefore, the function which calls itself is known as a recursive function.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *