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

C function parameters and return values

王林
Release: 2023-08-27 17:49:06
forward
803 people have browsed it

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();
}
Copy after login

Output

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

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);
}
Copy after login

Output

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

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);
}
Copy after login

Output

This function is taking 10 as argument, but returns nothing
Copy after login
Copy after login

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);
}
Copy after login

Output

This function is taking 10 as argument, but returns nothing
Copy after login
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template