140 likes | 285 Views
Function in C. What is function? Look the example…. #include< stdio.h > void main( ) { int x,y ; float w; char ch ; scanf (“%d % d”,&x,&y ); scanf (“%f ”,&w); scanf (“% c”,&ch ); printf (“x=%d, y=%d<br> w=%f <br> ch =%c<br>”, x,y,w,ch ); }. #include< stdio.h > void fun( ) { int x,y ;
E N D
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
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
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
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.
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( ) { }
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)
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); }
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); }
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( ); }
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); }
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); }
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); }