All C functions can be called either with arguments or without arguments in a C program. These functions may or may not return values to the calling function. Now, we will see simple example C programs for each one of the below.
- C function with arguments (parameters) and with return value
- C function with arguments (parameters) and without return value
- C function without arguments (parameters) and without return value
- C function without arguments (parameters) and with return value
S.no | C function | syntax |
1 | with arguments and with return values | int function ( int ); // function declaration function ( a ); // function call int function( int a ) // function definition {statements; return a;} |
2 | with arguments and without return values | void function ( int ); // function declaration function( a ); // function call void function( int a ) // function definition {statements;} |
3 | without arguments and without return values | void function(); // function declaration function(); // function call void function() // function definition {statements;} |
4 | without arguments and with return values | int function ( ); // function declaration function ( ); // function call int function( ) // function definition {statements; return a;} |
Note:
- If the return data type of a function is “void”, then, it can’t return any values to the calling function.
- If the return data type of the function is other than void such as “int, float, double etc”, then, it can return values to the calling function.
1. Example program for with arguments & with return value in C language:
In this program, integer, array and string are passed as arguments to the function. The return type of this function is “int” and value of the variable “a” is returned from the function. The values for array and string are modified inside the function itself.
Output:
***values before modification*** value of a is 20 value of arr[0] is 10 value of arr[1] is 20 value of arr[2] is 30 value of arr[3] is 40 value of arr[4] is 50 value of str is “fresh2refresh”
***values after modification***
value of a is 40 value of arr[0] is 60 value of arr[1] is 70 value of arr[2] is 80 value of arr[3] is 90 value of arr[4] is 100 value of str is “modified string” |
2. Example program for with arguments & without return value in C language:
In this program, integer, array and string are passed as arguments to the function. The return type of this function is “void” and no values can be returned from the function. All the values of integer, array and string are manipulated and displayed inside the function itself.
Output:
value of a is 20
value of arr[0] is 10
value of arr[1] is 20 value of arr[2] is 30 value of arr[3] is 40 value of arr[4] is 50
value of str is “fresh2refresh”
|
3. Example program for without arguments & without return value in C language:
In this program, no values are passed to the function “test” and no values are returned from this function to main function.
Output:
values : a = 50 and b = 80
|
4. Example program for without arguments & with return value in C Language:
In this program, no arguments are passed to the function “sum”. But, values are returned from this function to main function. Values of the variable a and b are summed up in the function “sum” and the sum of these value is returned to the main function.
Output:
Sum of two given values = 130
|
Do you know how many values can be return from functions in C language?
- Always, Only one value can be returned from a function.
- If you try to return more than one values from a function, only one value will be returned that appears at the right most place of the return statement.
- For example, if you use “return a,b,c” in your function, value for c only will be returned and values a, b won’t be returned to the program.
- In case, if you want to return more than one values, pointers can be used to directly change the values in address instead of returning those values to the function.
No comments:
Post a Comment