Types Of Function In C Programming msbte

Types Of Function In C Programming msbte

Types Of Function In C Programming msbte
Types Of Function In C Programming msbte


    Types Of Function In C 

    A function may fall in one of the categories given below. To which category the function belongs to is decided based on the absence or presence of arguments and whether the function returns any value or not.

    Four types of function in C are as follows :-
    1. Function with no return type and no Parameters [i.e Arguments]
    2. Function with No Return Value and with Parameters [i.e Arguments]
    3. Function with return type and no Parameters [i.e Arguments]
    4. Function with return Value and with Arguments

    Function with no return type and no Parameters [i.e Arguments]

    • When the function does not return a value void is written in place of the return type.
    • So general form of declaring the function with no return type and no arguments is given below.
    • Syntax :
      • void function name (void);
    • To call the function with no return type and no arguments following format is used:
      • function_name ();
    • An empty parenthesis indicates that the function call doesn't pass any parameter.

    Function with No Return Value and with Parameters [i.e Arguments]

    • When the function doesn't return any value void is written at the place of return type. And when a function has arguments, these arguments are written within parenthesis. So general form of declaring the function with no return type but with arguments is given below.
    • Syntax :
      • void function_name (argument_list)
    • To call the function with no return type but with arguments following format is used:
      • function_name ( list_of_actual_parameters);
    • Every parameter is separated by comma in the list.Parameter list should match the order, type and number of parameters as given in function prototype or definition
    • Example :
    • For example, if prototype of function is like:
      • void sum(int a, int b, int c);
    • Following all are the valid function calls 
      1. sum(5,10,20);
      2. int x=5, y=10, z=20; 
      3.         sum(x.y,z);
      4. sum(x,y-20);
      5. sum(x+5,y-5,2);
    • All are valid because the function sum() has three arguments of type integer. We can pass the constants, variables, expressions, or any combination of these as function arguments.
    • When expressions are used as function arguments then first the expression is solved and corresponding result is passed and mapped with formal parameter. As shown above first argument of sum will be x+5=5+5=10, second argument of sum will be y-5=10-5=5, and third is z ie. 20 is passed.

    Function with return type and no Parameters [i.e Arguments]

    • When the function does not have any arguments we use void in the parenthesis. And when the function returns a value, the data type for that value is written in the place of return type. So general form of declaring the function with return type but no arguments is given below.
    • Syntax :
      • return_type function_name (void);
    • To call the function with return type but no arguments following format is used:
    • variable_name = function_name ()
    • variable_name is used to hold the value returned by the function. Empty parenthesis indicates that the function doesn't have arguments.
    • Example :
    • 1. If the function returns meaningful value it can be stored in the variable or can be printed directly on the screen like:
      • printf("%d", function_name());
    • %d is used if want to print a int type number. Otherwise corresponding access specifier is used to print the return value.
    • 2. If prototype of function is like.
      • int mul(void);
    • then, the following all are the valid function calls
      1. int result= mul();
      2. printf("%d", mul());
      3. z = 5 + mul();
    • We can use a variable to store value returned by the function , or without creating new variable we can print the returned value by taking the function call as argument of printf() function.The return value of the function acts as the operand of expression.

    Function with return value and with Arguments

    • When the function has arguments and also returns a value, then the function it uses following general form to declare the function
    • Syntax :
      • return_type function name (argument_list);
    • To call the function with return value and with parameters following format is used:
      • variable_name = function_name (list_of_parameters)
    • variable_name is used to hold the value returned by the function. The type of variable should match the type of returned value.
    • The function parameters in list are separated by comma. Parameters in the list have same order, type and number of parameters as in the prototype.
    • Example :
    • If prototype of function is like.
      • int sub(int a, int b)
    • then, the following all are the valid function calls
      1. int result= sub(4,7);
      2. int x=4, y=7;                                                                                        result= sub(x, y);
      3. printf("%d", sub(x,y+1));
      4. z = 5 + sub(2,y);
    • We can use a variable to store value returned by the function 
    • The arguments of function can be constants ,variables , of expressions .
    • Another way is to call the function with return type and arguments  Where the value is printed directly by passing the function call as argument to printf() function.
    • The returned value of the function acts as the operand of expression.

    Post a Comment

    Previous Post Next Post