Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.

Similar presentations


Presentation on theme: "Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions."— Presentation transcript:

1 Functions 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 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 Different type of functions Functions with no arguments and no return values. Functions with arguments and no return values. Functions with arguments and return values. 15

16 Functions with no arguments and no return values #include void sum(); void main() { sum(); } void sum() { int a,b,c; printf("\nEnter 2 Numbers:"); scanf("%d%d",&a,&b); c=a+b; printf("Sum=%d\n",c); } 16

17 Functions with arguments and no return values #include void sum (int x, int y) void main() { int a, b; printf(“\nEnter two numbers:”); scanf(“%d%d”,&a,&b); sum(a,b); } void sum (int x, int y) { int sum=0; sum=x+y; printf(“sum=%d”,c); } 17

18 Functions with arguments and return values #include int sum(int x,int y); void main() { int a,b,c; printf("\nEnter 2 Numbers:"); scanf("%d%d",&a,&b); c=sum(a,b); printf("Sum=%d\n",c); } int sum(int x,int y) { return(x+y); } 18

19 Programming Practices-Assignment 5 Write a program to find factorial of number using functions Write a program to find even or odd using function Write a program to print average of N elements using functions 19


Download ppt "Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions."

Similar presentations


Ads by Google