user defined function

21
USER DEFINE USER DEFINE FUNCTIONS FUNCTIONS

Upload: king-kavin-patel

Post on 20-Jul-2015

62 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: user defined function

USER DEFINE USER DEFINE FUNCTIONSFUNCTIONS

Page 2: user defined function

• Definition - function•

●A set of statements working together with• common goal is known as function.•

●Also known as subprograms which are used to• compute a value or perform a specific task.•

●They can’t run independently and are always

• called by the main() program or by some other• function.

2

Page 3: user defined function

Functions

Function:

The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.

Function

Library User define

function function

Page 4: user defined function

Library function:

The library function are not required to be written by us.

Eg:

printf

scanf

Page 5: user defined function

User define function:

User defined functions are self-contained blocksof statements which are written by the user tocompute or perform a task.●They can be called by the main programrepeatedly as per the requirement.

Eg: main()

‘main’ is specially used function in C. Ever program must have main function to indicate, where the program begins its execution. When a program to large and complex then the result of debugging testing and maintaining becomes difficult.

Page 6: user defined function

Main program

Function A function B functionC

B1 B2

Page 7: user defined function

Using FunctionsUsing Functions• The main functions and other library functions does

need to be declared and defined but the main function’s body need to be defined by the programmer.

Page 8: user defined function

The 3 components associated The 3 components associated with functions are:with functions are:

1. The Declaration2. The function definition3. The Calling Statement

Page 9: user defined function

Function DeclarationFunction Declaration• In C user- written functions should normally be

declared prior to its use to allow compiler to perform type checking on arguments used in its call statement.

• The general form is:

• Retirn_data_type function_name (data_type Var_name, …..);

Page 10: user defined function

Function name: this is the name given to the function. It follows the same naming convention as that of any valid variable in C.

Return data type: this specifies the type of data given back to the calling construct.

Data type list: this list specifies the data type of each variables, the values of which are expected to be transmitted to the function. These variables are known as formal parameters.

Page 11: user defined function

Note:Note:• It is possible not to declare functions prior to the use

but the error checking will not be performed.

• ; is required at the end of function declaration.

• E.g. int FindMax(int x, int y);

Page 12: user defined function

Function DefinitionFunction Definition The collection of program statements that does a

specific tasks done by the function is called the function definition.

It conist of function header:◦ Int FindMax(int x, int y)◦ {◦ }

and function body. Int FindMax(int x, int y)

{ //body of the function…. }

Page 13: user defined function

FLOW OF FUNCTIONFLOW OF FUNCTION

• ●When the program is executed (that is, run) execution always begins at• the first statement in the function main no matter where it is placed in the• program.• ●Other functions are executed only when they are called.• ●Function prototypes appear before any function definition, so the• compiler translates these first. The compiler can then correctly translate a• function call.• ●A function call statement results in the transfer of control to the first• statement in the body of the called function.• ●After the last statement of the called function is executed, the control is• passed back to the point immediately following the function call.• ●A value-returning function returns a value. Therefore, for value-returning• functions, after executing the function when the control goes back to the• caller, the value that the function returns replaces the function call• statement.

Page 14: user defined function

Function callFunction call• The function is called from the main()• The function can in turn call a another function.• the function call statements invokes the function, which

means the program control passes to that function. Once the function completes its task, the program control is passed back to the calling environment.

• The general form of calling stmt is:• Function_name (var1, var2,..);

• Or

• var_name=function name(var1, var2,..);

Page 15: user defined function

Salient points to be taken Salient points to be taken into considerationinto consideration

• The func name and the type and number of arguments must match with that of the function declaration stmt and the header of the function definition.

• Arguments present in the form of expression are evaluated and converted to the type of formal parameters at the beginning of the body of the function.

Page 16: user defined function

Passing of arguments to Passing of arguments to the functionthe function

1. Call by value or pass by value: 1. When arguments are passed by values this means that local copies of

the values of the arguments are passed to the function.

2. Call by reference or pass by reference.1. The address of the variable is passed as value of parameters to the

function.

Page 17: user defined function

Passing arrays to functions

• Arrays can also be the arguments of function• Only the base address of the array is passed to the

function• Hence the passing of arguments is done by

reference.• When arrays is passed as arguments then actual

contents of the arrays is altered.

Page 18: user defined function

Recursive Functions.Recursive Functions.• Recursion in programming is a technique for

defining a problem in terms of one or more smaller versions of the problem.

• A recursive function is one that calls itself directly or indirectly to solve a smaller version of its task until a final call which does not require a self call.

Page 19: user defined function

The mechanics of The mechanics of recursive callrecursive call

• Start main program• …..• 1st call to print backward• enter a char : H

o 2nd call to print_backward• enter a char: i.• 3rd call to print_backward• enter a char: .• //now it will not call againcoz its ‘.’

• 3rd call finish• print i.• 2nd call finish• Print H. First call Finish……End of main program………

Page 20: user defined function

How recursion is How recursion is implemented.implemented.

• The storage mechanism in most modern languages is stack storage mgmt.

• In this mechanism the program’s data area is allocated at load time.

• While storage for functions data is allocated when function is invoked.

• On exit from function this storage is de-allocated.• This results in a runtime stack. • In recursive cases one call does not overlap with

the other.

Page 21: user defined function

What is need for What is need for implementing recursionimplementing recursion

• Decomposition into smaller problems of same size.• Recursive call must diminish problem size.• Necessity of base case.• Base case must be reached.