C Function Argument and Return Values
Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the C programming language, written functions may be declared with or without any arguments. When these functions are called for execution, they may or may not return any values as the return type is not expected to be void. Still, since there are no arguments passed by us while calling it from the primary program function, we haven't sent any argument values through the call we take here.
Functions in C
In layman's terms, a Function in C programming language is a block of code that will only run or which only executes when it is called from the primary main itself or any other secondary function.
Syntax of Function in C Programming Language
void this_is_my_Function() {
// code to be executed
}
int main(){
this_is_my_Function() //call the function in main program
return 0;
}
Code Example
//We are Creating a function
void myFunction() {
printf("Function has just got executed!");
}
int main() {
myFunction(); //We are call the function from here
return 0;
}
// Outputs "Function has just got executed!"
Output
Function has just got executed!
C Function Argument and Return Values
1.Writing functions with no arguments and no return values
Syntax and Declarations
Declaring the functions : void function();
Calling the function : function();
Defining the function :
void function()
{
statements;
}
Code
//Writing down the C programming language code for
//function with arguments and no return values
#include <stdio.h>
void value(void);
void main()
{
value();
}
void value(void)
{
float year_here = 2, period_is_here = 2;
float amount_or_money = 52000, inrate_we_have_is = 0.188;
float sum;
sum = amount_or_money;
while (year_here <= period_is_here) {
sum = sum * (1 + inrate_we_have_is);
year_here = year_here + 1;
}
printf(" The total money that we are having is %f:", sum);
}
Output
The total money that we are having is 61776.000000:
2. Writing functions with argument passing but without any return values
Syntax and Declarations
Declaring the functions : void function(int);
Calling the function : function(x);
Defining the function :
void function(int x)
{
statements;}
Code Example
//Writing down the C programming code for the function
//with argument but without any return value
#include <stdio.h>
void function(int, int[], char[]);
int main()
{
int abcd = 20;
int arrr[5] = { 110, 11120, 330, 2240, 5110 };
char strrr[30] = "javaTpoint.com";
function(abcd, &arrr[0], &strrr[0]);
return 0;
}
void function(int abcd, int* arrr, char* strrr)
{
int i;
printf("value of abcd we have is %d\n\n", abcd);
for (i = 0; i < 5; i++) {
printf("value of arrr[%d] is %d\n", i, arrr[i]);
}
printf("\nvalue of strrr is %s\n", strrr);
}
Output
value of abcd we have is 20
value of arrr[0] is 110
value of arrr[1] is 11120
value of arrr[2] is 330
value of arrr[3] is 2240
value of arrr[4] is 5110
value of strrr is javaTpoint.com
3. Functions which are having no arguments passed but returns a value
Syntax and Declarations
Declaring the functions : int function();
Calling the function : function();
Defining the function :
int function()
{
statements;
return x;
}
Code Example
//Writing down the C language code for function types with no arguments
// but which can return value upon code execution
#include <math.h>
#include <stdio.h>
int sum_fun();
int main(){
int number;
number = sum_fun();
printf("\nSum of two given values we have is = %d", number);
return 0;
}
int sum_fun()
{
int abcd = 23250, bcde = 2180, sum_fun;
sum_fun = sqrt(abcd) + sqrt(bcde);
return sum_fun;
}
Output
Sum of two given values we have is = 199
4. Functions which have arguments passed and can also return the values to the function call
Syntax and Declarations
Declaring the functions : int function ( int );
Calling the function : function( x );
Defining the function :
int function( int x )
{
statements;
return x;
}
Code Example
#include <stdio.h>
#include <string.h>
int function(int, int[]);
int main(){
int i_variable, abcd = 826328362839179191739191;
int array[5] = { 29479,2727,88765,17173,1111111 };
abcd = function(abcd, &array[0]);
printf("value of abcd is %d\n", abcd);
for (i_variable = 0; i_variable < 2; i_variable++) {
printf("value of array[%d] is %d\n", i_variable, array[i_variable]);
}
return 0;
}
int function(int abcd, int* array)
{
int i_variable;
abcd = abcd + 20;
array[0] = array[0] + 50;
array[1] = array[1] + 50;
return abcd;
}
Output
value of abcd is 1790010187
value of array[0] is 29529
value of array[1] is 2777