The document describes four types of functions in C programming: 1) No arguments and no return type, 2) No arguments with a return type, 3) With arguments and no return type, and 4) With arguments and with return type. Each type is illustrated with example code demonstrating how to implement and call the functions. The examples include user input and comparisons to determine the largest of two numbers.
The document describes four types of functions in C programming: 1) No arguments and no return type, 2) No arguments with a return type, 3) With arguments and no return type, and 4) With arguments and with return type. Each type is illustrated with example code demonstrating how to implement and call the functions. The examples include user input and comparisons to determine the largest of two numbers.
2)No arguments and with return type 3)with arguments and no return type 4) with arguments and with return type
1) No arguments No return value
#include<stdio.h> void max(); int main() { int n1,n2; printf("\ncalling function 1st time"); max(); // function call printf("\ncalling function 2nd time"); max(); //function call } void max() { int num1,num2; printf("\nenter first number : "); scanf("%d",&num1); printf("\nenter second number : "); scanf("%d",&num2); if(num1>num2) printf("%d is largest",num1); else printf("%d is largest",num2); }
2)No arguments and with return type
// c program to take a number for five times from input using function //No arguments but a return val #include<stdio.h> int getnumber(); int main() { int num; printf("\ncalling function"); for(int i=0;i<5;i++) { int num=getnumber(); printf("\nthe number is %d",num); }
} getnumber() { int a; printf("\nenter a number"); scanf("%d",&a); return a; }
3)with arguments and no return type
#include<stdio.h> void max(int num1,int num2); int main() { int n1,n2; printf("\nenter first number : "); scanf("%d",&n1); printf("\nenter second number : "); scanf("%d",&n2); printf("calling function"); max(n1,n2); // actual arguments int a,b; max(a,b); // garbage values // max(10,20,30,40); // ERROR in GCC too many arg to the function max // max(10); // Too few arg } void max(int num1,int num2) // formal arguments void max(int n1,int n2) { // n1=56; // Error : undeclared variable if(num1>num2) printf("\n %d is largest",num1); else printf("\n %d is largest",num2); }
4) with arguments and with return type
#include<stdio.h> int max(int num1,int num2); int main() { int n1,n2; printf("\nenter first number : "); scanf("%d",&n1); printf("\nenter second number : "); scanf("%d",&n2); int m=max(n1,n2); printf("\nmax=%d",m); } int max(int num1,int num2) { if(num1>num2) return num1; else return num2; }