TRENDING TOPICS

×

Types of C Functions

In C programming, Function can be divided in two broad categories which are listed below:

  1. Standard Library Functions.
  2. User Defined Function.
Types of C Functions

Standard Library Functions

Standard Library functions are predefined built-in functions which can be sued to perform several tasks like I/O processing, in string handling, to perform mathematical operations and many more..

How to use Standard Library Functions

Standard Library functions are defined in different – different header files, to use standard library function we have to include the header file in our program. On including header file in our program the functions which are defined in that header file are available for use and we can use those functions in our program.

For example scanf(), printf(), getch() etc. these are standard library functions, these functions are defined in “ stdio.h” header file. We will learn more about standard library function in upcoming topics one by one.

Note: You can also create header file and can define some function in header files, latter if we required such kind of function then we just can include out header file and we can use our pre-defined function. latter we will learn how to create header file and how to use them in our program.

Important

Before to start user defined function we should know about arguments and parameters, so let’s know what is arguments and parameters in c programming.

Arguments

To receive input value in function we can define input variable within the function’s (), these variable called as arguments. Arguments are defined at the time of function defining. See below example.

Arguments Example

void sum( int a, int b )
{
 // function body
}

In above example, variable a and b are called as arguments of function sum, variable a and b are int type, we can define any numbers of arguments of any types.

This is very simple to understand, when we call a function we can send some value to the function, to receive those value we requires some variable, these variable can be defined within function’s () and these variables are called as arguments.

Parameter

Parameters are the values passed to function when we call any function which contains argument in function definition.

Note: Parameters and arguments are connected to each other i.e. if we have 5 arguments in function definition then, when we call the function we need to send exact same numbers of parameters in same order and data-type should me match for each arguments and parameters

Parameter Example

void main()
{
   sum(5,6)
}

In above example, Passing two int type value 5 and 6 to the function sum() at the time of function call in main() function.

User Defined Functions

A Function which is defined by user is called as user defined function. We have understood function return, argument and parameters, if you are not clear on these points then before proceeding further we recommends you to first clear those points and then proceed ahead.

User Defined function can be categorised in 4 types, which are listed below.

  1. Function with no return no arguments.
  2. Function with no return but with arguments.
  3. Function with return but no arguments.
  4. Function with return and with argument.

Note: If you know about return and argument in depth then these sub categories are no more, types of user defined functions are just the game of return and arguments arrangement.

A. Function with no return no arguments

This kind of user defined function does not contain any arguments and any return value, basically these functions return type is void.

Example

Output

Intered the radius value as 12:

Enter Radius of Circle
12
Circle Area = 452.16

Explanation

In above example the function area() is a user defined function with no return and no arguments. The function area() return type is void so it will not return any value. Since within function area()’s () there is no variable defined so it has no arguments.

B. Function with no return but with arguments

The user defined function which contains arguments but does not return any value is called as function with no return but with arguments.

Example

Output

Intered the radius value as 10:

Enter Radius of Circle
10
Circle Area = 314

Explanation

In above program the function area() has one argument of float type, so when we call this function then function call requires a value of type float as parameter which is r in above example. The function area() return type is void so it will not return any value.

C. Function with return but no arguments

An user defined function which returns a value but it does not have any variable as argument is called as Function with return but no argument.

Example

Output

Intered the radius value as 8:

Enter Radius of Circle
8
Circle Area = 200.96

Explanation

  • In above example you can see the return type of function area() is float, so it is must to return a value of kind float otherwise program will give an error.
  • When we call function area() in main function’s scope, it returns a float value and we required a variable of float type to hold this value.
  • ar is a float type variable which is used to store the value returned by function area().
  • Simply we are assigning variable ar by the value return by function area() and we are printing variable ar value which is circle area in this program.

D. Function with return and with argument

User defined functions which have return value and arguments are called function with return and with arguments.

Example

Output

Intered the radius value as 15:

Enter Radius of Circle
15
Circle Area = 706.5

Explanation

  • The above example is example of function which takes input and return value
  • First user inter a value for radius which is stored in r variable and and it passed as parameter of function area().
  • After calculation the function area() return a float value and this value is used to assign variable ar
  • In last we print the value present in variable ar

Advertise With Us

We offer sponsored posts and guest posts with do-follow links, back-links, and other advertising services, To advertise email us at "advertise@gdatamart.com"

© 2021 GDATAMART.COM (All Rights Reserved)