Function calling is the component of the function used to call any user or library defined function inside main.
Syntax
return_type function_name(input1_dataType,input2_dataType);
Function calling has several types on the basis of input and output parameter –
— Function call with no input and no output
— Function call with input but no output
— Function call with no input but some output
— Function call with some Input and some output
— return_type : void
— input1,input2 : void
Syntax
function_name();
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#include<stdio.h> void addition(void); /*function prototype declaration*/ int main() { addition(); /*function calling*/ return 0; } void addition() /*function definition */ { unsigned int num1; unsigned int num2; unsigned int sum; printf("\nEnter Num1 :"); scanf("%d",&num1); printf("\nEnter Num2 :"); scanf("%d",&num1); sum=num1+num2; printf("sum=%u",sum); } |
1 2 3 4 5 |
Output: Enter Num1 :120 Enter Num2 :130 sum=250 |
return_type : void
input1 , input2 : variable or some constant value
Syntax
function_name(input1,input2,…..inputn);
Example
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 |
#include<stdio.h> void armstrong(unsigned int); /*function prototype declaration*/ int main() { unsigned int num; printf("\nEnter any three digit number :"); scanf("%u",&num); armstrong(num); /*function calling*/ return 0; } void armstrong(unsigned int numb) /*function definition */ { unsigned int numb_temp,r,sum=0; numb_temp=numb; while(numb_temp>0) { r=numb_temp%10; sum=sum+(r*r*r); numb_temp=numb_temp/10; } printf("\nsum of cubes of digits of %ld is= %ld",numb,sum); if(numb==sum) { printf("\nSo %ld is armstrong",numb); } else { printf("\nSo %ld is Not armstrong",numb); } } |
1 2 3 4 5 |
Output: Enter any three digit number :153 sum of cubes of digits of 153 is= 153 So 153 is armstrong |
return_type : some data type
input1,input2 : No input param
Syntax
result=function_name();
Example:
main function with no input parameter and an integer return.
1 2 3 4 5 6 7 8 |
#include<stdio.h> int main() { return 0; /*output is zero*/ } |
return_type: any data type
input1,input2 : value
Syntax
function_name(input1,input2);
Example:
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 |
#include<stdio.h> void addition(void); /*function prototype declaration*/ int main() { unsigned int num1; unsigned int num2; unsigned int sum; printf("\nProgram to add two unsigned integers :"); printf("\nEnter Num1 :"); scanf("%d",&num1); printf("\nEnter Num2 :"); scanf("%d",&num1); sum = addition(num1,num2); /*function calling*/ printf("sum=%u",sum); return 0; } /*function definition*/ unsigned int addition(unsigned int num1_copy,unsigned int num2_copy) { unsigned int sum; sum=num1_copy+num2_copy; return sum; } |
1 2 3 4 5 |
Output: Program to add two unsigned integers : Enter Num1 :120 Enter Num2 :130 sum=250 |