1 / 13

Function in C

Function in C. What is function? Look the example…. #include&lt; stdio.h &gt; void main( ) { int x,y ; float w; char ch ; scanf (“%d % d”,&amp;x,&amp;y ); scanf (“%f ”,&amp;w); scanf (“% c”,&amp;ch ); printf (“x=%d, y=%d<br> w=%f <br> ch =%c<br>”, x,y,w,ch ); }. #include&lt; stdio.h &gt; void fun( ) { int x,y ;

zoltan
Download Presentation

Function in C

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. Function in C

  2. What is function? Look the example… #include<stdio.h> void main( ) { intx,y; float w; char ch; scanf(“%d %d”,&x,&y); scanf(“%f ”,&w); scanf(“%c”,&ch); printf(“x=%d, y=%d\n w=%f \n ch=%c\n”, x,y,w,ch); } #include<stdio.h> void fun( ) { intx,y; float w; char ch; scanf(“%d %d”,&x,&y); scanf(“%f ”,&w); scanf(“%c”,&ch); printf(“x=%d, y=%d\n w=%f \n ch=%c\n”, x,y,w,ch); } void main( ) { fun( ); } Declaration function Calling function name

  3. What is function? Look the example… #include<stdio.h> void fun( ); void main( ) { fun( ); } void fun( ) { intx,y; float w; char ch; scanf(“%d %d”,&x,&y); scanf(“%f ”,&w); scanf(“%c”,&ch); printf(“x=%d, y=%d\n w=%f \n ch=%c\n”, x,y,w,ch); } prototype function Calling function name Declaration function

  4. Syntax of function Return typefunction_name (parameters or arguments ) { statements (what is the function do); } Return type may be void or int or float or char or string… Parameters may be no parameter or take one or more variable

  5. How to write a function? From the previous examples we declare a function not return any value. So, we wrote a void before the name of function without ; after parantheces. Function name must be written with the standard rules to declare a variable. Because fun( ) function not return a value, when we call it in the main there is only one way. What is it? Eliminate return type. Then take the function name and putted in main followed by ;. In the previous example function didn’t have any parameter.

  6. How to write a prototype function? Take the first line of function followed by; without { and } The declaration function must be written after the end of main function. Like this: void FF( ); void main( ) { } void FF( ) { }

  7. Void function with parameter When we declare a function with arguments must be care to take the same order of arguments when function was called. Why we use parameters? To transfer the value of variables from main or other functions to variables in another functions.(like a pipe)

  8. continue • Let be rewrite the first program in these slides but by using parameters. #include<stdio.h> void fun( int a, int b, float c, char d) { printf(“x=%d, y=%d\n w=%f \n ch=%c\n”, a,b,c,d); } void main( ) { intx,y; float w; char ch; scanf(“%d %d”,&x,&y); scanf(“%f ”,&w); scanf(“%c”,&ch); fun(x, y, w, ch); }

  9. continue • Let be rewrite the first program in these slides but by using parameters and prototype. #include<stdio.h> void fun( int a, int b, float c, char d); void main( ) { intx,y; float w; char ch; scanf(“%d %d”,&x,&y); scanf(“%f ”,&w); scanf(“%c”,&ch); fun(x, y, w, ch); } void fun( int a, int b, float c, char d) { printf(“x=%d, y=%d\n w=%f \n ch=%c\n”, a,b,c,d); }

  10. example Write a program by using function to add two numbers and print the result. #include <stdio.h> void SUM( ) { inta,b; a=2; b=4; printf(“sum= %d \n”, a+b); } void main( ) { SUM( ); }

  11. example Write a program by using function to add two numbers and print the result. #include <stdio.h> void SUM( ); void main( ) { SUM( ); } void SUM( ) { inta,b; a=2; b=4; printf(“sum= %d \n”, a+b); }

  12. example Write a program by using function to add two numbers and print the result. #include <stdio.h> void SUM(int x, int y) { printf(“sum= %d \n”, x+y); } void main( ) { inta,b; a=2; b=4; SUM(a,b); }

  13. example Write a program by using function to add two numbers and print the result. #include <stdio.h> void SUM(int x, int y) ; void main( ) { inta,b; a=2; b=4; SUM(a,b); } void SUM(int x, int y) { printf(“sum= %d \n”, x+y); }

More Related