Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions in C Computer Programming(1)- 1090CS Manesh T

Similar presentations


Presentation on theme: "Functions in C Computer Programming(1)- 1090CS Manesh T"— Presentation transcript:

1 Functions in C Computer Programming(1)- 1090CS Manesh T maneshpadmayil@gmail.com

2 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions Different types of functions Flow of Execution

3 Define Functions 3 A function is program segment which performs a well defined task Big programs are divided into small programs

4 4 Advantages of Using Functions 1.Reduce size of main program 2.Easy to understand 3.Easy error handling

5 5 Standard (Predefined) Functions Predefined functions Part of the C language Provided in function libraries abs(x), sin(x), log(x), pow( x, n) Examples: abs(x), sin(x), log(x), pow( x, n) These functions will return a value y = pow (3, 4.5); To be assigned y = pow (3, 4.5); 3.14 * sqr(r) To be used in an expression 3.14 * sqr(r) #include Make sure to use the required #include file

6 6 Predefined Functions

7 Model 7 #include void main ( )printf is the name of a predefined {function in the stdio library printf (“Hello World!\n”) ; this statement is is known as a } function call this is a string we are passing as an argument (parameter) to the printf function

8 Elements of a function Function Definition Function Call Function Declaration 8

9 Function Definition Function Type, Name, list of parameters Local variable declaration Function statements Return statements 9

10 Format of the function definition Data type fun_name( type1 arg1,……typen arg n) { Local variables declarations function statement1; function statement2; return Statement; } 10 Function Type Name of function List of parameters (formal arguments) Return statement

11 Example int sum(int a, int b) { int sum=0; sum=a+b; return (sum); } 11 Function Type Name of function List of parameters (formal arguments) Return statement

12 Function Call-format Name_function(arg1, arg2,……..,arg n) Eg: int c; c=sum(a, b); 12 List of parameters (Actual Parameters)

13 Function Declaration Type Name_fun(type 1 arg1,… typen argn); Eg: int sum(int a, int b); 13

14 14 Examining printMessage #include void printMessage ( ) ; function declaration void main ( ) { printMessage ( ) ; function call } void printMessage ( )function header { printf (“A message for you:\n\n”) ; function printf (“Have a nice day!\n”) ; body } function definition

15 Model Programs Write a program to add two numbers using functions Write a program to find average of three numbers using functions Write a program to find factorial of number using functions Practice: Develop by yourown Write a program to find even or odd using function

16 Write a program to add two numbers using functions #include int sum(int x,int y) { int z; z=x+y; return(z); } void main() { int a,b,c; printf("\nEnter two numbers: "); scanf("%d%d",&a,&b); c=sum(a,b); printf("\nSum=%d",c); } 16

17 Write a program to find average of three numbers using functions #include float average(int x,int y,int z) { float av; av=(float)(x+y+z)/3; return(av); } void main() { int a,b,c; float avg; printf("\nEnter three numbers: "); scanf("%d%d%d",&a,&b,&c); avg=average(a,b,c); printf("\nAverage=%f",avg); } 17

18 Write a program to find factorial of number using functions #include int factorial(int n) { int i,f=1; for(i=1;i<=n;i++) { f=f*i; } return(f); } void main() { int i,n,fact=1; printf("\nEnter n:"); scanf("%d",&n); fact=factorial(n); printf("\nFactorial=%d\n",fact); }

19 #include void main() { } int n,f; printf("Enter n:"); scanf("%d",&n); f=factorial(n); printf("\nFactorial=%d\n",f); int factorial(int n) { int i,fact=1; for(i=1;i<=n;i++) { fact=fact*i; } return(fact); } Function n fact

20 Model Questions from Functions Define Functions? See Slide No 3 What are advantages of functions? See Slide No 4 Explain function definition with example? See Slide No 10 & 11 Explain Function call with example? See Slide No 12 20


Download ppt "Functions in C Computer Programming(1)- 1090CS Manesh T"

Similar presentations


Ads by Google