Home  >  Article  >  Backend Development  >  C function parameters and return values

C function parameters and return values

王林
王林forward
2023-08-27 17:49:06765browse

C function parameters and return values

Here we will see the different types of C functions based on return values ​​and parameters.

Therefore, a function can take some parameters, or not take any parameters. Likewise, a function can return something and otherwise return nothing. Therefore, we can classify them into four types.

  • A function with no parameters and no return type.
  • A function that has no parameters but returns something.
  • A function that takes parameters but returns nothing.
  • A function that takes parameters and returns 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.

This function does not accept any input parameters, and the return type is void. Therefore, it returns nothing.

Example

#include <stdio.h>
int my_function() {
   printf("This function takes no argument, But returns 50</p><p>");
   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

This function accepts an input parameter, but its return type is void. So it returns nothing.

Example

#include <stdio.h>
int my_function(int x) {
   printf("This will take an argument, and will return its squared value</p><p>");
   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

The function here accepts any input parameter and returns a value.

The above is the detailed content of C function parameters and return values. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete