1 / 19

TYPES OF FUNCTIONS

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.

leda
Télécharger la présentation

TYPES OF FUNCTIONS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. TYPES OF FUNCTIONS M.S.P.V.L. Polytechnic College, Pavoorchatram.

  2. 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

  3. 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.

  4. 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*/ }

  5. 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; }

  6. 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.

  7. 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*/ }

  8. 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; }

  9. 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; }

  10. 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.

  11. Example main() { int a,b; -------- -------- message(); } message() { int x,y value; ------- value=x+y return(value) }

  12. 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

  13. 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.

  14. 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*/ }

  15. 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); }

  16. 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); }

  17. 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); } }

  18. 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(); }

  19. The End Thank you

More Related