Cpr Transperencies - Ch 04 Functions

  • November 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Cpr Transperencies - Ch 04 Functions as PDF for free.

More details

  • Words: 1,052
  • Pages: 17
Chapter no. 4 Functions and Structures

Main Program

Function A

Function B

B1

Function C

B2

Functional Programming in ‘C’

-1-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Flow of control in multifunction program

main( ) { ---------------------------------------

function1( ); ---------------------------------------

function2( );

---------------------------------------

function1( ); } function1( ) { }

----------------------------------------------------------

function2( ) { --------------------

function3( ); --------------------

} function3( ) { }

----------------------------------------------------------

-2-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

User defined functions. #include<stdio.h> message( ) { printf(“Hello”); } main( ) { message( ); }

#include<stdio.h> void message( ) { printf(“Hello”); } void main( ) { message( ); } -3-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Declaration of many functions.

#include<stdio.h> poly( ) { printf(“I am in polytechnic”); } engg( ) { printf(“I am in engineering”); } agri( ) { printf(“I am in agri”); } main( ) { printf(“I am in main”); poly( ); engg( ); agri( ); }

-4-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Some Conclusions • Any C Program must contain at least one function. • If program contains only one function it must be main( ). • There is no limit on the number of functions present in a C program. • Each function in a program is called in the sequence specified by functions call in main( ). • After each function has done its thing, control returns to main( ). When main( ) run out of function calls program ends.

-5-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Scope, Visibility and Lifetime of Variables Scope:

In what region of program, the variable in actually available for use. 1.

Local Scope (internal variables)

2.

Global Scope (external variables)

Visibility:

Accessibility of a variable from memory. Lifetime / Longevity:

The period during which a variable retains its given value during execution of program.

-6-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Storage Classes 1. Where the variable would be stored? 2. What will be the initial value of the variable? (i. e. default initial value). 3. What is scope of the variable? 4. What is lifetime of the variable?

Types: 1. Automatic 2. Register 3. Static 4. External

-7-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

1. Automatic Storage class

They are created when the function is called and destroyed automatically when the function is exited. Storage:

Memory.

Default initial Value:

Garbage.

Scope:

Local (to block in which variable defined)

Lifetime:

Till control remains within block where it is defined.

Example: main() { auto int x, y; x = 10; printf(“Values : %d %d”, x, y); }

-8-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

2. Register Storage class

We can tell the compiler that a variable should be kept in one of the microprocessor’s register, instead of keeping in memory. Storage:

CPU Registers.

Default initial Value:

Garbage.

Scope:

Local (to block in which variable defined)

Lifetime:

Till control remains within block where it is defined.

Example: main() { register int a; for(a=0;a<10;a++) printf(“\nValues : %d ”, a); }

-9-

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

3. Static Storage class

The value of the variable remains as it is until the end of the program. Storage:

Memory.

Default initial Value:

Zero.

Scope:

Local (to block in which variable defined)

Lifetime:

Value of the variable remains as it is between function calls.

Example: void insert() { static int m; m++; printf(“\n%d”, m); } main() { insert(); insert(); insert(); }

- 10 -

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

4. External Storage class

Accessing the value of the variable which is defined somewhere in the program globally. Storage:

Memory.

Default initial Value:

Zero.

Scope:

Global

Lifetime:

As long as the program execution doesn’t come to an end.

Example: main() { extern int y; printf(“Value: %d ”, y); } int y = 30;

- 11 -

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Flowchart for function main() { int x, y, z; printf(“Enter values: ”); scanf(“%d%d”, &x, &y); z = mul(x, y); printf(“Multiplication: %d”, z); } void mul(int a, int b) { int m; m = a * b; return(m); } Start

int mul(a, b)

Accept values of x and y Let, m = a Let, z = mul(x, y)

×b

return (m)

Print value of z

Stop

- 12 -

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Recursion 1. Direct Recursion Function call function( )

2. Mutual Recursion Function call function1( )

function2( )

3. General Indirect Recursion function2( ) function1( )

function2( )

- 13 -

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Factorial of a number using recursion

6! = 6 * 5!

120 * 6 = 720

5! = 5 * 4!

24 * 5 = 120

4! = 4 * 3!

6 * 4 = 24

3! = 3 * 2!

3*2=6

2! = 2 * 1!

2*1=2

Base: 1! = 1

- 14 -

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Factorial in C using recursion #include<stdio.h> #include int fact(int x)

Return Point

{ if(x<1) return(1); else

return( x * fact(x-1)); } void main() { int a; a = fact(4); printf("%d", a); getch(); } - 15 -

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

Finding XY in C using recursion #include<stdio.h> #include int power(int x, int y) {

Return Point

if(y<1) return(1); else return( x * power(x,--y)); } void main() { printf("%d", power(4, 3)); getch(); }

- 16 -

By, Kute T. B. for CPR (FYIF) 2007-08

Chapter no. 4 Functions and Structures

How Recursion executes?

1 2 * fact (1) 3 * fact (2) 4 * fact (3) 5 * fact (4) 6 * fact (5) 7 * fact (6) Result

- 17 -

By, Kute T. B. for CPR (FYIF) 2007-08

Related Documents