unit iii. functions to divide a big program into a number of relatively smaller and easily...

23
UNIT III

Upload: harry-charles

Post on 13-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

UNIT III

Page 2: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

FunctionsTo divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a function.A function is defined to be a self-contained program which is written for the purpose of accomplishing some task.Function can be categorize into two types:

1.Build-in functions

2.User-defined functions.

Build-in functions are those which are already made available as part of C Library. They can be used by any programmer.scanf(),printf(),strlen(),scrcpy() are some of the built-in functions.

A used-defined function is one which is written by a user to solve a problem.

Page 3: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Advantage of function

1.Minimization of memory space usage: The repetition of the same code to more consumption of memory space. This problem can be avoided by defining a separate function.

2.Improvization of overall organization of a program: The overall organization of a program is improved way of compartmentalization or modularization. A program is divided into a number of subprograms. Each subprogram is viewed as a separate compartment or a module.

3.Facilitation of team work:The projects will be divided into a number of modules and each module is assigned to a different programmer.The programmer implement their modules using functions,will be integrated together at the final stage.

4.Simplification of software engineering task like testing and debugging: Identifying the location of the errors and correcting them the flexibility provided by functions.

Page 4: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Syntax:

return-type function-name(arguments)

{ local variables; statements; return(expression);

}

where,

return-type -> refers to the data type of the value being returned by the function.

function-name-the function referred to while using function.

The function-name and arguments declaration enclosed within a pair of parentheses is usually referred to as the Function Header.

The function header is followed by the local variables declaration part

and a set of executable statements of the function enclosed within a pair of opening brace and closing brace referred to as the Function Body.

Page 5: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Function Header+Function Body=Function Definition

A function which invokes another function is called calling function and function invoked is termed called function.

function can be classified into three types:

1.Functions with no arguments and no return value

2.Functions with arguments and no return value

3.Functions with arguments and return value

Page 6: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Function with no arguments and no Return Value

In function f() which does not require arguments to be passed, and does not return any value to its calling program. The function definition will have the following form:

void f()

{

variables;

statements;

}

The function f() does not require arguments and does not return any value back to its calling program, there is no data communication between the function f() and its calling function.

Page 7: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Example:#include<stdio.h>

void largest()

{

int a,b,c,l;

printf(“Enter three numbers \n”);

scanf(“d%d%d”,&a,&b,&c);

l=a;

if(b>l)

l=b;

if (c>l)

l=c;

printf(“Largest=%d\n”,l); Output:

} 3 4 5

void main() Largest=5

{

largest();

}

Page 8: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Functions with arguments and no Return Value

The f() which requires arguments to be passed, and does not does not return any value to its calling program. The function definition will have the following form:

void f(arguments)

{

variables;

statements;

}

The function f() requires arguments and does not return any value back to its calling program, there is one-way data communication from the calling function to the function f().

Page 9: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Example:#include<stdio.h>

void largest(int a,int b,int c)

{

int l;

l=a;

if(b>l)

l=b;

if(c>l)

l=c;

printf(“Largest=%d \n”,l);

}

void main()

{

int a,b,c;

int x,y,z;

printf(“Enter values of a,b and c \n”);

scanf(“%d%d%d”,&a,&b,&c);

largest(a,b,c);

printf(Enter values of x,y and z \n”);

scanf(“%d%d%d”,&x,&y,&z);

largest(x,y,z);

getch();

}Output:

Enter the values of a,b and c

3 4 5

Largest=5

Enter the values of x,y and z

6 3 2

Largest=6

Page 10: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Functions with arguments and no Return Value

The f() which requires arguments to be passed, and also returns a value to its calling program. The function definition will have the following form:

void f(arguments)

{

variables;

statements;

return(Expression);

}

The function f() requires arguments and also returns a value back to its calling program, there is two-way data communication between the calling function and the function f().

Page 11: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Example:#include<stdio.h>

void largest(int a,int b,int c)

{

int largest;

largest=a;

if(b>largest)

largest=b;

if(c>largest)

largest=c;

return(largest);

}

void main()

{

int a,b,c,lar;

int x,y,z;

printf(“Enter values of a,b and c \n”);

scanf(“%d%d%d”,&a,&b,&c);

lar=largest(a,b,c);

printf(“Largest=%d \n”,lar);

printf(“Enter values of x,y and z \n”);

scanf(“%d%d%d”,&x,&y,&z);

lar=largest(x,y,z);

printf(“Largest=%d\n”,lar);

}Output:

Enter the values of a,b and c

3 4 5

Largest=5

Enter the values of x,y and z

4 5 6

Largest=6

Page 12: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Recursive Functions Recursion is basically a method a function calling itself. This function involved in the process is referred to as a recursive function.

Example:

/To find factorial number */#include<stdio.h>int fact(int)

void main()

{

int number,f;

printf(“Enter a number \n”);

scanf(“%d”,&number);

f=fact(number);

printf(“fact=%d \n”,f);

}

int fact(int number){ int f; if(number==0) return(1); else f=number*fact(number-1); return f;}Output: Enter a number4factorial of 4=24 4!=4*3 =4*3*2 =4*3*2*1 =24

Page 13: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

/* To generate fibbonaci series */#include<stdio.h>int fib(int)void main(){ int n,I; printf(“Enter a number \n”); scanf(“%d”,&); for(i=1;i<=n;i++) printf(“%d \n”,fib(i));} int fib(int n){ int f; if(n==1) return 1; else if(n==2) return 1; else f=fib(n-1)+fib(n-2); return f;}

Enter a number501s123

Page 14: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Scope of Variables

Scope of variable is define to the area of its existence in a program. Scope determines the “lifetime” of the variable id defined to the duration of time for which variable exists.

Depending on where variables are declared, fall into two categories:

1.Local variable

2.Global Variable

Local Variable The variable declared within a function is called a local variable. The

value of the local variables are valid only within the function are declared.

The local variables which are defined in one function have no relationship in another function.

Local variable are created when a function is invoked and they are destroyed when the function is exited.

Page 15: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Global VariableThis variables that are declared prior to all functions are called global variables and can bed used by all the modules in the program. These variables are retained till the life time of the program.Two global variables can have same name.

Page 16: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Storage ClassesThe storage class determines the part of member storage is allocated for an object and how long the storage allocation continues to exit.Storage class tells us:

1) Where the variable is stored.

2) Initial value of the variable.

3) Scope of the variable. Scope specifies the part of the program which a variable is accessed.

4) Life of the variable.

There are four types of storage classes:

1) Automatic storage class

2) Register storage class

3) Static storage class

4) External storage class

Page 17: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

AUTOMATIC STORAGE CLASS• In this automatic storage class. Variable is stored in memory.• Default value is garbage value.• Scope is local to the block.• Life is, with in the block in which the variable is defined.Example 1:void main(){ auto int i=10; printf(“%d”,i); }Output:10Example 2:void main(){ auto int i; printf(“%d”,i); }Output:1002

Page 18: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

•In example 1, i value is initialized to 10.So,output is 10.

•In example 2, i value is not initialised.So,compiler reads i value is a garbage value.

REGISTER STORAGE CLASS

•Variable is stored in CPU registers.

•Default value is garbage value.

•Scope is local to the block.

•Life is, with in the block in which the variable is defined.

•We can not use register storage class for all types of variables.

For example:

•register float f;

•register double d;

•register long l;

Page 19: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Example

void main()

{

register int i=10;

printf(“%d”,i);

}

Output

10

STATIC STORAGE CLASS

• Variable is stored in memory.

• Default value is zero.

• Scope is local to the block.

• Life is, value of the variable persists between different function calls.

Page 20: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Example

void main()

{

add();

add();

}

add()

{

static int i=10;

printf(“\n%d”,i);

i+=1;

}

Output

10

11

Page 21: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

EXTERNAL STORAGE CLASS

• Variable is stored in memory.

• Default value is zero.

• Scope is local to the block.

• Life is, as long as the program execution doesn’t come to an end.

Note:

In given example program,i declared in two places.One is above the main(),second is within main().Within main() i is used in within main() only.Before main() i is used to outof the main() functions.i.e, display() uses the i value as 10.

Page 22: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

Example :

int i=10;

void main()

{

int i=2;

printf(“%d”,i);

display();

}

display()

{

printf(“\n%d”,i);

}

Output:

2

10

Page 23: UNIT III. Functions  To divide a big program into a number of relatively smaller and easily manageable subprograms. Each subprogram in C is called a

NOTE:

• Static and auto storage classes both are different in the case of life. Remaining all r same.

• In the case of recursive function calls we use static storage class.

• Register storage classes are used in the case of frequently used variables. Because these are stored in CPU registers.

• Extern storage class for only those variables are used almost all functions in the program. This would avoid unnecessary passing of the variables.