Top Banner
TMC1414/TMC1413 INTRODUCTION TO PROGRAMMING Lecture 06 Function
27

TMC1414/TMC1413 Introduction To Programming

Feb 10, 2016

Download

Documents

elana

TMC1414/TMC1413 Introduction To Programming. Lecture 06 Function. OBJECTIVES. In this chapter you will learn: To construct programs modularly from small pieces called functions. The common math functions available in the C Standard Library. To create new functions. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: TMC1414/TMC1413 Introduction To Programming

TMC1414/TMC1413INTRODUCTION TO PROGRAMMINGLecture 06 Function

Page 2: TMC1414/TMC1413 Introduction To Programming

OBJECTIVESIn this chapter you will learn: To construct programs modularly from

small pieces called functions. The common math functions available in

the C Standard Library. To create new functions. The mechanisms used to pass information

between functions. Simulation techniques using random num

ber generation. How to write and use recursive functions,

i.e., functions that call themselves.2

Page 3: TMC1414/TMC1413 Introduction To Programming

INTRODUCTION Divide and conquer

Construct a program from smaller pieces or components These smaller pieces are called modules

Each piece more manageable than the original program

3

Page 4: TMC1414/TMC1413 Introduction To Programming

PROGRAM MODULES IN C Functions

Modules in C Programs combine user-defined functions with

library functions C standard library has a wide variety of functions

Function calls Invoking functions

Provide function name and arguments (data) Function performs operations or manipulations Function returns results

Function call analogy: Boss asks worker to complete task

Worker gets information, does task, returns result Information hiding: boss does not know details

4

Page 5: TMC1414/TMC1413 Introduction To Programming

5Hierarchical boss function/worker function relationship.

Page 6: TMC1414/TMC1413 Introduction To Programming

MATH LIBRARY FUNCTIONS Math library functions

perform common mathematical calculations #include <math.h>

Format for calling functions FunctionName( argument );

If multiple arguments, use comma-separated list printf( "%.2f", sqrt( 900.0 ) );

Calls function sqrt, which returns the square root of its argument

All math functions return data type doubleArguments may be constants, variables, or

expressions6

Page 7: TMC1414/TMC1413 Introduction To Programming

MATH LIBRARY FUNCTIONS

Include the math header by using the preprocessor directive #include <math.h> when using functions in the math library.

7

Page 8: TMC1414/TMC1413 Introduction To Programming

Function Description Example

sqrt( x ) square root of x sqrt( 900.0 ) is 30.0

sqrt( 9.0 ) is 3.0 exp( x ) exponential function ex exp( 1.0 ) is 2.718282

exp( 2.0 ) is 7.389056

log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0

log( 7.389056 ) is 2.0 log10( x ) logarithm of x (base 10) log10( 1.0 ) is 0.0

log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0

fabs( x ) absolute value of x fabs( 5.0 ) is 5.0

fabs( 0.0 ) is 0.0 fabs( -5.0 ) is 5.0

ceil( x ) rounds x to the smallest integer

not less than x ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0

8

Commonly used math library functions. (Part 1 of 2.)

Page 9: TMC1414/TMC1413 Introduction To Programming

Function Description Example

floor( x ) rounds x to the largest integer not greater than x

floor( 9.2 ) is 9.0

floor( -9.8 ) is -10.0 pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128.0

pow( 9, .5 ) is 3.0 fmod( x, y ) remainder of x/y as a floating-

point number fmod( 13.657, 2.333 ) is 1.992

sin( x ) trigonometric sine of x

(x in radians) sin( 0.0 ) is 0.0

cos( x ) trigonometric cosine of x

(x in radians) cos( 0.0 ) is 1.0

tan( x ) trigonometric tangent of x

(x in radians) tan( 0.0 ) is 0.0

9

Commonly used math library functions. (Part 2 of 2.)

Page 10: TMC1414/TMC1413 Introduction To Programming

FUNCTIONS Modularize a program

All variables defined inside functions are local variables Known only in function defined

Parameters Communicate information between functions Local variables

Benefits of functions Divide and conquer

Manageable program development Software reusability

Use existing functions as building blocks for new programs

Abstraction - hide internal details (library functions) Avoid code repetition

10

Page 11: TMC1414/TMC1413 Introduction To Programming

FUNCTIONSIn programs containing many functions, main is often implemented as a group of calls to functions that perform the bulk of the program’s work.

11

Each function should be limited to performing a single, well-defined task, and the func tion name should effectively express that task. This facilitates abstraction and promotes software reusability.

Programs should be written as collections of small functions. This makes programs easier to write, debug, maintain and modify.

Page 12: TMC1414/TMC1413 Introduction To Programming

FUNCTIONS

12

void main(){ int a=10,b=20,m; int big(int x, int y);

m=big(a,b);

printf(“Max=%d”,m);

}

int big(int x, int y){ if(x>y) return(x); else return(y);}

Page 13: TMC1414/TMC1413 Introduction To Programming

FUNCTIONS

13

void main(){ int a=10,b=20,m int big(int x, int y);

m=big(a,b);

printf(“Max=%d”,m);

}

int big(int x, int y){ if(x>y) return(x); else return(y);}

10 20a b m

Page 14: TMC1414/TMC1413 Introduction To Programming

EXAMPLE: TO FIND THE LARGER BETWEEN TWO NUMBERS

14

void main(){ int a=10,b=20,m int big(int x, int y);

m=big(a,b);

printf(“Max=%d”,m);

}

int big(int x, int y){ if(x>y) return(x); else return(y);}

10 20a b

10 20 x y

20big

20m

Page 15: TMC1414/TMC1413 Introduction To Programming

WRITE YOUR OWN FUNCTION Function definition

Two principal componentslong int factorial(int n){ int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } } return(prob);}

Page 16: TMC1414/TMC1413 Introduction To Programming

WRITE YOUR OWN FUNCTION Function definition

Two principal components First line

Contains data type to be returned Contains the function name A set of arguments (optional)

long int factorial(int n){ int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } } return(prob);}

Page 17: TMC1414/TMC1413 Introduction To Programming

WRITE YOUR OWN FUNCTION Function definition

Two principal components First line

Contains data type to be returned Contains the function name A set of arguments (optional)

Body of function Contains expression statements for

the operation required If data type of the function had been

defined, should return more then one functions.

long int factorial(int n){ int i, long int prob = 1; if(n > 1) { for( i = 2; i <= n; ++i) { prob *= i; } } return(prob);}

Page 18: TMC1414/TMC1413 Introduction To Programming

FUNCTION PROTOTYPES Function can be defined

with first line and body However, compiler may

confuse the function name and cause error of unknown variable or function

Function prototypes is introduced to alert the compiler regarding existence of function

#include <stdio.h>

main(){

int a;printf("\na = ");scanf("%d", &a);

d = cubebyvalue(a);printf("\n\n maximum =

%d", a);

return 0;}

int cubebyvalue(int x){

return(x * x * x);}

int cubebyvalue(int);

Page 19: TMC1414/TMC1413 Introduction To Programming

#include <stdio.h>

int cubebyvalue(int x){

return(x * x * x);};

main(){

int a;printf("\na = ");scanf("%d", &a);

d = cubebyvalue(a);printf("\n\n maximum = %d",

a);

return 0;}

Page 20: TMC1414/TMC1413 Introduction To Programming

Accessing function Usually known as calling a function A function can be called by its name (from first

line). Two type of function access

Call by reference Call by value

Page 21: TMC1414/TMC1413 Introduction To Programming

Two type of function accessCall by value

Copy of argument passed to function

Changes in function do not effect original

Use when function does not need to modify argument

Avoids accidental changes

21

#include <stdio.h>

int cubebyvalue(int);

main(){

int a,d;printf("\na = ");scanf("%d", &a);

d = cubebyvalue(a);printf("\n\n%d,%d", a,d);

return 0;}

int cubebyvalue(int x){

return(x * x * x);}

Page 22: TMC1414/TMC1413 Introduction To Programming

Call by reference Passes original

argument Changes in function

effect original Only used with trusted

function

#include <stdio.h>

void cubebyreference(int*);

main(){

int a,d;printf("\na = ");scanf("%d", &a);

cubebyreference(&a);

printf("\n\n %d,%d", a,d);

return 0;}

void cubebyreference(int *xPtr){

*xPtr =*xPtr * *xPtr * *xPtr;}

Page 23: TMC1414/TMC1413 Introduction To Programming

RECURSION Recursive functions

Functions that call themselvesCan only solve a base caseDivide a problem up into

What it can do What it cannot do

What it cannot do resembles original problem The function launches a new copy of itself (recursion

step) to solve what it cannot doEventually base case gets solved

Gets plugged in, works its way up and solves whole problem 23

Page 24: TMC1414/TMC1413 Introduction To Programming

#include <stdio.h>

int factorial(int n);

main(){

int n;

printf("n = ");scanf("%d", &n);

printf("n! = %d\n", factorial(n));

}

int factorial(int n){

if (n<=1){

return 1;}else{

return(n * factorial(n-1));}

}

Page 25: TMC1414/TMC1413 Introduction To Programming

5! = 5 * 4 * 3 * 2 *1 4! = 4 * 3 * 2 * 1 3! = 3 * 2 * 1 2! = 2 * 1 1! = 1

• 5! = 5 * 4!• 4! = 4 * 3!• 3! = 3 * 2!• 2! = 2 * 1!• 1! = 1

factorial(5)in main

Factorial(5)

15*4! 4*3! 3*2! 2*1

Factorial(4)

5

Factorial(3)

4

Factorial(2)

3

Factorial(1)

2

return(n*factorial(n-1))

Page 26: TMC1414/TMC1413 Introduction To Programming

Check this out on youtube. http://www.youtube.com/watch?v=iOS5sPivuJ

A&NR=1&feature=fvwp

Page 27: TMC1414/TMC1413 Introduction To Programming

# END OF SESSION#

27

Thank You