200 likes | 435 Views
TYPES OF FUNCTIONS. M.S.P.V.L. Polytechnic College, Pavoorchatram. Types. Function with no arguments and no return values Functions with arguments and no return values Function with no arguments and return values Functions with arguments and return values.
E N D
TYPES OF FUNCTIONS M.S.P.V.L. Polytechnic College, Pavoorchatram.
Types • Function with no arguments and no return values • Functions with arguments and no return values • Function with no arguments and return values • Functions with arguments and return values
Function with no arguments and no return values • This is the simplest function. • This does not receive any arguments (data) from the calling function and does not return any value to the calling function.
Example main() { int a, b; ------- ------- message(); /*No actual arguments to send*/ ------- ------- } void message() /*No formal argument to receive*/ { ------- ------- return; /*No return value to the called function main*/ }
Program Write a program to find the sum of two numbers using function with no arguments and no return values. #include<stdio.h> main() { int sum(); sum(); } sum() { int i,j,x; scanf(“%d%d”,&i,&j); x=i+j; printf(“sum=%d”,x); return; }
Function with arguments and no return values • This function receives arguments (data) from the calling function and does not return any value to the calling function.
Example main() { int a,b; ------- ------- message(a,b); /*actual arguments to sent*/ -------- -------- } void message(x,y) /*formal arguments to receive*/ int x,y; /*formal arguments declaration*/ { -------- -------- return; /*no return value to the called function main*/ }
Program 1. Write a program to add two numbers using function with arguments and no return values. #include<stdio.h> main() { int sum(int, int); int i,j; scanf(“%d%d”, &i,&j); sum(i,j); } sum(x,y) int x,y; { int z; z=x+y; printf(“sum=%d”,z); return; }
2. Write a program to find the square of first n natural numbers using function with arguments and no return values #include<stdio.h> main() { void square(int); int i,n; printf(“enter the value of n \n”); scanf(“%d”, &n); for(i=1;i<n;i++) square(i); } square(m); int m; { int temp; temp=m*m; printf(“%d/t%d/n”,m,temp); return; }
Function with no arguments and return values • This function does not receive arguments (data) from the calling function and return the computed value back to the calling function.
Example main() { int a,b; -------- -------- message(); } message() { int x,y value; ------- value=x+y return(value) }
write a ‘c’ program to find a square root of a number using a function. #include<stdio.h> #include<conio.h> #include<math.h> main() { float sqt(); float sqt(); float y; y=sqt(); printf(“\n square root value is:%f”,y); getch(); return; } /*function with no arguments and return value*/ float sqt() { float x; printf(“\n enter the number:”); scanf(“%f”,&x); return(sqrt(x)); } OUTPUT: Enter the number: 9 Square root value is: 3.000
Functions with arguments and return values • These functions receive arguments (data) from the calling function and return the computed value back to the calling function.
Example main() { int a,b; ------ ------ message (a,b); /*actual argument to send*/ } message (x,y) /*formal arguments to receive*/ int x,y; /*formal arguments declaration*/ { int vlaue; ----- value=x+y; return(value); /*this statement returns the value to the calling function*/ }
Programs • Write a program to add two numbers using function with arguments and return values. #include<stdio.h> main() { int sum(int,int); int i,j,value; scanf(“%d%d”,&i,&j); value=sum(i,j); printf(“sum=%d”,value); } sum(x,y) int x,y; { int z; z=x+y; return(z); }
2. Write a program to find the square of numbers using function with arguments and return values #include<stdio.h> main() { int square(int); int i,n,value; printf(“enter the value of n \n”); scanf(“%d”,&n); for (i=0;i<=n;i++) { value=square(i); printf(“%d%d”,i,value); } } square(m) int m; { int temp; temp=m*m; return(temp); }
3.Write a program to find the factorial of a given number using function with arguments and return values. #include<stdio.h> main() { int fact(int); int a,n; printf(“enter any integer number\n”); scanf(“%d”,&n); a=fact(n); printf(“the factorial as n=\n”,a); } fact(n) int n; { int vlaue i; value=1; if(n==0) return (value); else { for(i=1;i<n;i++) value=value*i; return(value); } }
4. Write a program to find whether the given year is leap or not #include<stdio.h> in isleap(int y) { if(y%4==0) return 1; else return 0; } main() { int year; printf(“\nenter the year:”); scanf(“%d”,&year); if(isleap(year)) printf(“\nthe given year is leap year”); else printf(“\n the given year is not leap year”); getch(); }
The End Thank you