function cpu

19
PREPARED BY :- DEVIN BABARIYA(33) UTKARSH DUBEY(34) KRUSHAL KAKADIYA(35) MEET SONI(37) PRIYAM BHATT(38) MILAN SUTHAR(39) Subject: FUNCTION

Upload: krushal-kakadia

Post on 22-Aug-2015

53 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: FUNCTION CPU

PREPARED BY : -

DEVIN BABARIYA(33)UTKARSH DUBEY(34)

KRUSHAL KAKADIYA(35)MEET SONI(37)

PRIYAM BHATT(38)MILAN SUTHAR(39)

Subject: FUNCTION

Page 2: FUNCTION CPU

INTRODUCTION

Every ‘C’ program is collection of functions. In every ‘C’ program, main() function must be there, because the execution of program starts from main() function.

Function is a group of statements as a single unit known by some name which is performing some well defined task.

The functions which are created by programmer in a program are called as a user-defined functions.

The functions which are readily available in library are called as library functions.

Page 3: FUNCTION CPU

Advantages of Functions

It facilitates top-down modular programming, whereby the large problem is divided into small parts and each small part is handled later in depth.

Function can be used in other program also, so labor is reduced.

It reduced the size of code, if that code is repetitively used in program.

It allows term-work for large project, individual members of term has to concentrate on function given to them and not on whole complex problem.

Page 4: FUNCTION CPU
Page 5: FUNCTION CPU

User defined functions

User defined functions are created for doing some specific task in a program. The code is written as one unit having a name.

As the size of program increases, It becomes difficult to understand.

The code of function is written only once in the program and called for any number of times from the program whenever required.

Page 6: FUNCTION CPU

Built-in functions

These are the functions which are readily available and placed in side the library.

Their prototypes are declared in various header files depending on the category to which they belong to.

We need to understand the prototype of built-in function and include that header file in our program.

Page 7: FUNCTION CPU

Function declaration

The syntax for declaring function is: type function_name(argument(s));Here, type specifies the type of value

returned, function_name specifies name of user defined function, and in bracket argument(s) specifies the arguments supplied to the function as comma separated list of variables.

The declaration of a function is terminated by semicolon (;) symbol. The declaration is also called as prototype of a function.

Page 8: FUNCTION CPU

Function Definition

The syntax for definition of function is: type function_name(argument(s)); { statement(s); } Here, type, function_name and argument(s) have same meaning as before. The statement(s) within { } symbols indicate the body of a function. Here, the actual logic of the work of function is implemented.If the return type is not mentioned, then by default it is taken as int. We can not define function inside a function. We can call one function from other. If the function return type is other than void, there must be return statement in the function. The returned value can be put in brackets but it is not compulsory.

Page 9: FUNCTION CPU

Category of functions

The function in ‘C’ language can be divided in following categories:

1) Functions with no arguments and no return value 2) Functions with arguments and no return value 3) Functions with arguments and with return value

When we have function which takes arguments, in that case we need to understand the formal parameters and actual parameters.

The variables or parameters used in definition of function are called as formal parameters. while, parameters used in the call to the functions are called as actual parameters.

Page 10: FUNCTION CPU

Scope of variables

By scope of variables, we mean in what part of the program the variable is accessible. There are two types of scope.

1.) local and 2.) GlobalDifference between local and global variables :

Local variables Global variables

Declared inside function body Declared outside body

Not initialized automatically Initialized automatic by value 0

Use of local variables advisable Too much use of global variables make program difficult to debug, so use with care

Page 11: FUNCTION CPU

Parameter passing to function

When a function is called from other function,the parameters can be passed in two ways.

1) Call by value 2) Call by reference1) Call by value : In call by value, argument value are

passed to the function , the contents of actual parameters are copied into the formal parameters. The called function can not change the values of the variables which are passed.

2) Call by reference : In call by reference, as the name suggests,

the reference of the parameter is passed to the function and not the value. Call by reference is used whenever we want to change the value of local variables declared in one function to be changed by other function

Page 12: FUNCTION CPU

Recursion

Sometimes, the function is defined in terms of itself.

Recursion is the process by which a function calls itself. Because the function calls itself , there must be some condition to stop recursion; otherwise it will lead to infinite loop.Types of recursion:Direct recursion :- In the case of direct recursion, a function explicitly calls.Indirect recursion :- The function calls another function, which ultimately calls the caller function.

Advantages of recursion : Easy solution for recursively defined problems. Complex programs can be easily written in less code.

Page 13: FUNCTION CPU

Parameters as array and string

As a array We can pass the whole array as a parameter to the

function. If we pass the name of an array as an argument, we are effectively passing whole array as an argument. Naturally, passing an address of an array is call by reference.As a string It is character array as parameter, because string is

stored in a single dimensional array of character type. We can write user defined functions to manipulate strings. In this section , we will understand how a string can be passed as a parameter to a function.

Page 14: FUNCTION CPU

Storage classes

The storage class of a variable determines the scope and lifetime of a variable. There are four storage classes.

1)Auto 2)Register 3)Static 4)External

Page 15: FUNCTION CPU

Auto

If we do not specify the storage specify with variable, by default it is auto. All the local variable have auto storage class. Auto variables are stored in memory, garbage initial value, scope local to function and lifetime till the control remains in that function. As the name suggests, auto variables are created when the function is called and destroyed automatically when the function terminates.

Page 16: FUNCTION CPU

Register

Variables which are to be stored in the registers of CPU are called as register variables. Register keyword is used for that purpose, for example,register int a;declaration the variables as a register variable.Storage of register variable take place in registers , have garbage value, scope local to function and are created when the function is called and destroyed automatically when the function terminates.

Page 17: FUNCTION CPU

Static

All the global variables are by default of static storage class. Default value of static variable is 0. static global variable is available throughout the entire file in which it is declared, while static local variable is available in the function in which it is defined, but is initialized only once and retains its value even when the control goes out of the function.Static variables are stored in memory, default initial value 0, scope is local to function or file in which declared and lifetime is entire program execution with retaining values between function calls.

Page 18: FUNCTION CPU

External

Normally, the scope of global variable starts from its definition up to the end of the program. So, the functions which are defined before the definition of global variable can not access it.

Storage in memory, default initial value 0, scope is global all the files of program and life is entire program execution.

Page 19: FUNCTION CPU

Thank you