What is function Look the example includelt stdioh gt void main int xy float w char ch scanf d dampxampy scanf f ampw scanf ID: 377208
Download Presentation The PPT/PDF document "Function in C" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Function in CSlide2
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\n w=%f \n ch=%c\n”, x,y,w,ch);}
#include<stdio.h>void fun( ){int x,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 nameSlide3
What is function? Look the example…
Declaration function
Calling function name
#include<stdio.h>void fun( );void main( ){ fun( );}void fun( ){int x,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 Slide4
Syntax of function
Return type
function_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 variableSlide5
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. Slide6
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( ){}Slide7
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)Slide8
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( ){ int x,y; float w;
char ch; scanf(“%d %d”,&x,&y); scanf(“%f ”,&w); scanf(“%c”,&ch); fun(x, y, w, ch);}Slide9
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( ){ int x,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);}Slide10
example
Write a program by using function to add two numbers and print the result.
#include <
stdio.h>void SUM( ) { int a,b; a=2; b=4; printf(“sum= %d \n”, a+b);}void main( ){ SUM( ); }Slide11
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( ) { int a,b; a=2; b=4; printf(“sum= %d \n”, a+b);}Slide12
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( ){ int a,b; a=2; b=4; SUM(a,b); }Slide13
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( ){ int a,b; a=2; b=4; SUM(a,b); } void SUM(int x, int y) { printf(“sum= %d \n”, x+y);}