C function argument and return values

CServer Side ProgrammingProgramming

Here we will see what are the different types of C functions based on the return values and arguments.

So a function either can take some arguments, or nothing is taken. Similarly, a function can return something, otherwise does not return anything. So we can categorize them into four types.

  • Function with No argument and No return type.
  • Function with No argument and Return something.
  • A function that takes argument but returns nothing.
  • Functions that take an argument and also return something.

Example

#include <stdio.h>
void my_function() {
   printf("This is a function that takes no argument, and returns nothing.");
}
main() {
   my_function();
}

Output

This is a function that takes no argument, and returns nothing.

Here this function is not taking any input argument, and also the return type is void. So this returns nothing.

Example

#include <stdio.h>
int my_function() {
   printf("This function takes no argument, But returns 50\n");
   return 50;
}
main() {
   int x;
   x = my_function();
   printf("Returned Value: %d", x);
}

Output

This function takes no argument, But returns 50
Returned Value: 50

Here this function is not taking any input argument, but its return type is int. So this returns a value.

Example

#include <stdio.h>
void my_function(int x) {
   printf("This function is taking %d as argument, but returns nothing", x);
   return 50;
}
main() {
   int x;
   x = 10;
   my_function(x);
}

Output

This function is taking 10 as argument, but returns nothing

Here this function is taking an input argument, but its return type is void. So this returns nothing.

Example

#include <stdio.h>
int my_function(int x) {
   printf("This will take an argument, and will return its squared value\n");
   return x * x;
}
main() {
   int x, res;
   x = 12;
   res = my_function(12);
   printf("Returned Value: %d", res);
}

Output

This function is taking 10 as argument, but returns nothing

Here this function is taking any input argument, and also returns value.

raja
Published on 02-May-2019 12:13:12
Advertisements