Prev Next
C Argument, return value:
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.
C functions aspects
|
syntax
|
1. With arguments and with return values |
function declaration: int function ( int );function call: function ( a );function definition: int function( int a ) { statements; return a; } |
2. With arguments and without return values |
function declaration: void function ( int );function call: function( a );function definition: void function( int a ) { statements; } |
3. Without arguments and without return values |
function declaration: void function();function call: function();function definition: void function() { statements; } |
4. Without arguments and with return values |
function declaration: int function ( );function call: function ( );function definition: int function( ) { 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 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
#include<stdio.h> #include<string.h> int function(int, int[], char[]); int main() { int i, a = 20; int arr[5] = {10,20,30,40,50}; char str[30] = "\"fresh2refresh\""; printf(" ***values before modification***\n"); printf("value of a is %d\n",a); for (i=0;i<5;i++) { // Accessing each variable printf("value of arr[%d] is %d\n",i,arr[i]); } printf("value of str is %s\n",str); printf("\n ***values after modification***\n"); a= function(a, &arr[0], &str[0]); printf("value of a is %d\n",a); for (i=0;i<5;i++) { // Accessing each variable printf("value of arr[%d] is %d\n",i,arr[i]); } printf("value of str is %s\n",str); return 0; } int function(int a, int *arr, char *str) { int i; a = a+20; arr[0] = arr[0]+50; arr[1] = arr[1]+50; arr[2] = arr[2]+50; arr[3] = arr[3]+50; arr[4] = arr[4]+50; strcpy(str,"\"modified string\""); return a; } |
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 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include<stdio.h> void function(int, int[], char[]); int main() { int a = 20; int arr[5] = {10,20,30,40,50}; char str[30] = "\"fresh2refresh\""; function(a, &arr[0], &str[0]); return 0; } void function(int a, int *arr, char *str) { int i; printf("value of a is %d\n\n",a); for (i=0;i<5;i++) { // Accessing each variable printf("value of arr[%d] is %d\n",i,arr[i]); } printf("\nvalue of str is %s\n",str); } |
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 this program, no values are passed to the function “test” and no values are returned from this function to main function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include<stdio.h> void test(); int main() { test(); return 0; } void test() { int a = 50, b = 80; printf("\nvalues : a = %d and b = %d", a, b); } |
Output:
values : a = 50 and b = 80
|
4. Example program for without arguments & with return value:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include<stdio.h> int sum(); int main() { int addition; addition = sum(); printf("\nSum of two given values = %d", addition); return 0; } int sum() { int a = 50, b = 80, sum; sum = a + b; return sum; } |
Output:
Sum of two given values = 130
|
Do you know how many values can be return from C functions?
- 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.
Continue on C – Library functions….
Continue on C – User defined functions & adding them in C library….