Presentation is loading. Please wait.

Presentation is loading. Please wait.

FUNCTIONS.

Similar presentations


Presentation on theme: "FUNCTIONS."— Presentation transcript:

1 FUNCTIONS

2 What is a Function ? Types of Functions
Function is a self- contained block or a sub-program of one or more statements that perform a specific task when called. Types of Functions 1. Library functions. 2. User-defined functions. Library functions These are the in- built functions of ‘C’ library. These are already defined in header files. e.g. printf (); is a function which is used to print ( ); at output. It is defined in ‘stdio.h ’file

3 Need for User defined functions
Programmer can create their own function in C to perform specific task. Need for User defined functions If we want to perform a task repetitively then it is not necessary to re-write the particular block of the program again and again .Shift the particular block of statements in a user-defined function. The function defined can be used any number of times to perform the task. Using functions large program can be reduced to smaller one called sub-programs. It is easy to debug and find out the errors in it.

4 Elements of User defined functions
Function definition Function call Function declaration Definition of function Function definition is also called function implementation. function name; function type; list of parameters; local variable declarations; function statements; a return statements. All the six elements are grouped into two parts: 1. Function header (first three elements) 2. Function body (second three elements)

5 A general format of a function definition:
function _type function _name (parameter list) { local variable declarations; executable statement1; executable statement2; executable statement3; ………………………… …………………………. return statements; }

6 Function declaration Function declaration is also called function prototype. It consist of 4 types: Function type Function name parameters list Terminating semicolon. general format function _type function _name (parameter list) ;

7 NOTE The parameter list must be separated by commas.
The parameter names do not need to be same in the prototype declaration and the function definition. Use of parameter names in the declaration is optional. If the function has no parameters , the list is written as (void). EX: int mul (int a,int b); void mul (void)

8 Function call A function can be called by simply using the function name followed by a list of parameters (or arguments) if any in parentheses, terminated by semicolon (;). void printline(void); //function declaration main() { printline(); //function call printf(" c functions\n"); printline(); getch(); } void printline(void) // function definition int i; for(i=1;i<40;i++) printf("-"); printf("\n"); return;

9 int mul(int a,int b); //function declaration
main() { int y; y = mul(10,2); //function call printf("%d\n",y); getch(); } int mul(int x,int y) //function definition int p; p=x*y; //called function return(p);

10 Types of Functions Functions with no arguments and no return values.
Functions with arguments and no return values. Functions with arguments and return values. Functions with no arguments and return values. Functions that return multiple values.

11 TYPE-1 Functions with no arguments and no return values

12 #include<stdio.h>
#include<conio.h> void printline(void); main() { printf("Welcome to functions in C"); printline(); printf("Function are easy to learn."); getch(); } void printline(void) int i; printf("\n"); for(i=0;i<30;i++) printf("*"); return;

13 TYPE-2 Functions with arguments and no return values

14 #include<stdio.h>
#include<conio.h> void add(int x, int y) ; main() { add(30,15); add(63,49); add(952,321); getch(); } void add(int x, int y) int result; result = x+y; printf("Sum of %d and %d is %d.\n\n",x,y,result); return;

15 TYPE-3 Functions with arguments and return values

16 #include<stdio.h>
#include<conio.h> int add(int x, int y); main() { int z; z = add(50,50); printf("Result %d.\n\n",z); printf("Result %d.\n\n",add(30,55)); getch(); } int add(int x, int y) int result; result = x+y; return(result);

17 TYPE-4 Functions with no arguments and return values

18 #include<stdio.h>
#include<conio.h> int send(void); main() { int z; z = send(); printf("\nYou entered : %d.", z); getch(); } int send(void) int no1; printf("Enter a no : "); scanf("%d",&no1); return(no1);

19 TYPE-5 Functions that return multiple values #include<stdio.h> #include<conio.h> main() { int a=20, b=20, p,q; calc(a,b,&p,&q); printf("Sum =  %d, Sub = %d",p,q); getch(); } void calc(int x, int y, int *add, int *sub) *add = x+y; *sub = x-y;

20 Two ways of passing arguments to the function
Call by value Call by reference Call by value In this type value of actual arguments are passed to the formal arguments and the operation is done on the formal arguments. Any change made in the formal arguments does not affect the actual arguments. When function is called by the call or by value method, it does not affect the actual content of the actual arguments.

21 Call by value #include <stdio.h> #include <conio.h> int swap(int,int) ; main() { int a=50, b=70; swap(a,b); printf ("a=%d b=%d", a, b); getch(); } int swap (int a1,int b1) int t; t=a1; a1=b1; b1=t; printf("a1=%d b1=%d\n",a1,b1); return;

22 Call by reference #include <stdio.h> #include <conio.h> int swap(int *a,int *b); main() { int x,y; printf("enter value of X and Y:"); scanf("%d %d",&x,&y); swap(&x,&y); printf("\n In MAIN X=%d Y=%d",x,y); getch(); } int swap(int *a,int *b) int k; k=*a; *a=*b; *b=k; printf("X=%d Y=%d",*a,*b);


Download ppt "FUNCTIONS."

Similar presentations


Ads by Google