P7_FunctionsInC

24

Transcript of P7_FunctionsInC

Page 1: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 1/24

Page 2: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 2/24

FUNCTIONSFUNCTIONS

C program is a collection of functions.

Block of statements grouped together to perform a

task. Any c program contains at least one function.

     main()

      printf() and scanf()-predefined function

Program execution always begin with main()

function.

Page 3: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 3/24

FUNCTIONFUNCTION -- mainmain

Features defined by the language are:

     Name

     Number of arguments

      Argument types

Statements inside the block are defined by

the programmer.

Other user-defined functions can be called

from main.

Page 4: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 4/24

FUNCTIONSFUNCTIONS

#include<stdio.h>

Main() /* control is passed to main() */

{

Printf(³Functions´);

}

Program with a function:#include<stdio.h>

Void printMesg()

{

Printf(³Welcome to world of functions \n´);

}

Void main()

{

Printf(³Functions \n´);

 printMesg();

}

Page 5: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 5/24

FUNCTIONSFUNCTIONS

Page 6: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 6/24

Why use FUNCTIONS?Why use FUNCTIONS?

Easy reusability of the same code.

Separating the code in to modules makes the

 program easy to design and understand.

FUNCTION handling in C Concepts associated with functions.

     

Function declaration or function prototype     Function definition

     Combination declaration and function definition

     Passing arguments

     Return statement

     Function calls

Page 7: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 7/24

FUNCTIONFUNCTION

Void main()

{

Void function1();

«.

«.Function1();

«.

«.

}

Void function1(){

«

«

}

Return type

Semicolon here

Function call

Function definition

Return type

Function declaration

Page 8: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 8/24

Function declarationFunction declaration

It provides the following information to the

compiler.      Name of the function

     Type of the value returned(optional,integer by default)

      Number and type of arguments

Syntax:return type function_name(type,type,«,type);

Example:float average(float ,float );

Page 9: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 9/24

Function definitionFunction definition

Starts with function declarator 

Function body

     Composed of statements

     Delimited by braces

Declarator and declaration must have the same features.

 No function definition is allowed within a function definition.

Example:

float average(float x , float y)

{

return (x+y)/2;

}

Page 10: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 10/24

Function declarationFunction declaration--EliminationElimination

Declarations can be placed anywhere in the program.

If the functions are defined before they are called, then the declaration is

unnecessary.

Consider an example:

#include<stdio.h>float average(float x , float y)

{

return (x+y)/2;

}

Void main()

{float a=12.75,b=1.456;

  printf(³Average is:%f´,Average(a,b))

}

Here function is defined before being called in the main, eliminating the necessity of prototype.

In large programs, it is preferable to declare functions in the beginning to avail flexibility.

Page 11: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 11/24

FUNCTIONFUNCTION--Return ValueReturn Value

May or may not return any value.

If it does not return a value,return type is void.

If it returns, the return type may be a valid datatype.

It provides the means of exiting from a function.

When the compiler encounters return statement,it resumes the calling function from the point in

which call occurred.

Page 12: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 12/24

FUNCTION CallFUNCTION Call

Function comes to life only, when a call is

made.

Syntax of function call;function_name(parameters);

When the compiler encounters a function call,

control of execution is passed to the particular function definition.

Page 13: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 13/24

FUNCTIONSFUNCTIONS--ExampleExample#include <stdio.h>

Float average(float , float); /*function declaration or prototype*/

int main(void)

{

float value1 = 0;

float value2 = 0;

float value3 = 0;

 printf("Enter two floating-point values separated by blanks: ");

scanf("%f %f", &value1, &value2);

value3 = average(value1, value2); /*function call*/

 printf("\nThe average is: %f\n", value3);

return 0;}

float average(float x, float y) /*function definition*/

{

return (x + y)/2; /* return value */

}

Page 14: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 14/24

FUNCTIONSFUNCTIONS--ExampleExample

Page 15: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 15/24

FUNCTION ParametersFUNCTION Parameters

Provides the means of communication between the

calling function and the called function.

Classified into:     Formal parameters - Given in the function declaration and definition

     Actual parameters ± Given in the function call

Conditions to be satisfied for a successful function

call are as follows:      Number of arguments in the function call and function declaration must

 be the same.

     Data type of each of the argument in function call and function

declaration must be in the same order.

Page 16: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 16/24

FUNCTION ParametersFUNCTION Parameters

C provides two mechanisms to pass

arguments.

     Pass arguments by value     Pass arguments by address or by pointer 

Page 17: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 17/24

FUNCTIONFUNCTION--Pass by valuePass by value

In calling function, contents are not changed,

even if they are changed in called function.

Contents of the variable is just copied to theformal parameter of the function definition.

In most of the cases, calling function refers to

main() function, and the called function refers

to the function called from main().

Let us consider an example.

Page 18: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 18/24

FUNCTIONFUNCTION--Pass by value examplePass by value example

main( )

{

int a = 10, b = 20 ;

swapv ( a, b ) ;

printf ( "\na = %d b = %d", a, b ) ;}

swapv ( int x, int y )

{

int t ;

t = x ;x = y ;

y = t ;

printf ( "\n x = %d y = %d", x, y ) ;

}

Output:x = 20 y = 10

a = 10 b = 20

Page 19: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 19/24

FUNCTIONFUNCTION--RecursionRecursion

It is possible for a function to call itself.

Recursive function must follow two rules:

     It must have an end-point.     It must make the problem simpler.

Factorial calculation is an typical example

of recursive function.

Page 20: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 20/24

RECURSIONRECURSION--ExampleExamplemain( ){

int a, fact ;

printf ( "\nEnter any number " ) ;

scanf ( "%d", &a ) ;

fact = rec ( a ) ;

printf ( "Factorial value = %d", fact ) ;

}

rec ( int x )

{

int f ;

if ( x == 1 )

return ( 1 ) ;

else

f = x * rec ( x - 1 ) ;

return ( f ) ;

}

Page 21: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 21/24

Page 22: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 22/24

RECURSIONRECURSION

Recursion function consists of four steps:

     Initialization-variables in the form of arguments

passed to the function.

     Decision-argument values are used to determinewhether further recursive calls are required.

     Computation-computation is performed using the

local variables and the parameters at the current

depth.     Update-update is done so that the variables can be

used for further recursive calls.

Page 23: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 23/24

I request Electronics and communication

ENGINEERING students to visit my blogfor 

more««

abhishek1ek.blogspot.com

awhengineering.blogspot.com

Page 24: P7_FunctionsInC

8/3/2019 P7_FunctionsInC

http://slidepdf.com/reader/full/p7functionsinc 24/24