C - Functions Category

C Programming

Share

In this tutorial we will learn about category of functions in C programming language.

Function Parameter and Function Argument

A parameter is a variable that we use in a function definition. An argument is the actual data that we pass to the function parameter.

Example:

#include <stdio.h>

//Function declaration
float getArea(float, float);

int main(void) {

  /**
   * We are calling the getArea() function
   * The arguments passed to the function are 10 and 20.
   */
  float area = getArea(10, 20);

  //output
  printf("Area: %f\n", area);

  return 0;
}

/**
 * Function definition
 * Following are the parameters of the given function getArea()
 * length
 * width
 */
float getArea(float length, float width) {
  return length * width;
}

In the above example the function arguments are 10 and 20 whereas, the function parameters are length and width.

So, argument value 10 is stored in the parameter name length and argument value 20 is stored in the parameter name width respectively.

Function categories

We have already learned about functions is the previous tutorial. Now, in this tutorial we will talk about four categories of functions.

  • Function with no argument and no return value
  • Function with no argument but returns a value
  • Function with argument but no return value
  • Function with argument and returns a value

Function with no argument and no return value

To create a function with no argument and no return value we set the parameter list to void and the return type of the function to void.

In the following example we have a function print10() that takes no argument and returns no value.

void print10() {
  printf("10");
}

Function with no argument but returns a value

For this type of functions we have void in the parameter list but we set the return type to match the type of value returned by the function.

In the following example we have function get10() which takes no argument but return an integer value 10 so, the return type of the function is set to int.

int get10(void) {
  return 10;
}

Another example of this is the main() function that we have used so far in the previous tutorials.

int main(void) {

  // some code goes here...

  return 0;
}

Function with argument but no return value

In this type of functions we have the return type set to void but the parameter list is set to accept some argument.

In the following example we have a function getNumber() which takes an integer value as argument but returns no value.

void getNumber(int num) {
  printf("Number: %d", num);
}

Function with argument and returns a value

In this type of functions we set both the parameter list and the return type.

In the following example we have a getArea() function which takes length and width of type float as argument and returns the area of type float.

float getArea(float length, float width) {
  return length * width;
}
Share