Functions Part 4-Introduction to Computer Programming-Lecture Slides, Slides of Computer Programming

Dr. Mehandi Nandakumar delivered this lecture at Baddi University of Emerging Sciences and Technologies for Introduction to Computer Programming course. Its main points are: Functions, Block, Code, Execution, Programs, Memory, C , Internal, External, Argument, Square, Roots

Typology: Slides

2011/2012

Uploaded on 07/13/2012

ekbaal
ekbaal šŸ‡®šŸ‡³

3

(1)

30 documents

1 / 52

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Wednesday, May 2, 2012
Computer Programming
Lecture 23
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34

Partial preview of the text

Download Functions Part 4-Introduction to Computer Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

Wednesday, May 2, 2012

Computer Programming

Lecture 23

ļ‚§

A function is a self contained block of codewith a specific purpose. ļ‚§

When a function is called, the function code isexecuted and when it is finished, the executionof the calling program continues immediatelyafter the point where the function was called. Functions

C++ Functions ļ‚§^

C++ allows the use of both internal (user-defined) and external functions. ļ‚§

External functions (e.g.,

abs

,^

ceil

,^

rand

sqrt, etc.) are usually grouped into specializedlibraries (e.g.,

iostream

,^

stdlib

,^

math

etc.)

5

Using functions –Math Library functions ļ‚§

C++ includes a library of Math functionsyou can use. ļ‚§

You have to know how to

call

these

functions before you can use them. ļ‚§

You have to know what they return. ļ‚§

You don’t have to know how they work!

7

x = sqrt(y); ļ‚§

The stuff we give to a function is called theargument(s).

Y

is the argument here.

A C++ function can’t change the value ofan argument! ļ‚§

If

y

was

^100

before we call

sqrt

, it will

always be

^100

after we call

sqrt

8

Table of square roots

int

i;

for

(i=1;i<10;i++)

cout

sqrt(i)

ā€œ\nā€;

But I thought we had to give

sqrt()

a double?

C++ does automatic

type conversion

for you.

10

Other Math Library Functions

ceil

floor

cos

sin

tan

exp

log

log

pow

fabs

fmod



A

function definition

consists of

two parts

:



The function Header:^ ā—¦^

data type of the return value; ā—¦^

name of the function; ā—¦^

and the function’s parameter list (enclosed in parenthesis),e.g. int square(int value);



The function Body:^ ā—¦^

consists of the code to be executed when the function iscalled.



If the function does not return any value then

void

may be used instead of return value. 

If the function does not take any parameters, then^ void

can be used within parenthesis to show this.

How to define a function?

// Program 8.1 Calculating powers………….. Continuedint main(){ cout << endl;// Calculate powers of 8 from -3 to +3for(int i = -3 ; i <= 3 ; i++)

cout <<power(8.0, i)<< "

";

cout << endl;return 0;}

// Program 8.1 Calculating powers: Output

1

8

64

512



Information is passed to a function using arguments by thecalling program. Argument is the actual value passed tothe function. 

The function definition contains the parameters list whichreflects the data type expected by the function. 

The arguments which are specified when calling a functionreplace the parameters that are used in definition. 

The data types of arguments and parameters are generallythe same (if they are different, implicit cast may takeplace). It is not necessary that the name should be thesame. 

If a return type is specified then the function returns asingle value of that type when its execution finishes.

Parameters, Arguments and Returnvalues.

  • Thursday, May 3,
    • Lecture

// Program 8.2 Calculating powers - rearranged#include using namespace std;double power(double x, int n);

// Prototype for power function

int main(){ cout << endl;// Calculate powers of 8 from -3 to +3for(int i = -3 ; i <= 3 ; i++)

cout <<power(8.0, i)<< "

";

cout << endl;return 0;}

// Program 8.2 Calculating powers – rearranged……………. Continued// Function to calculate x to the power ndouble power(double x, int n){ double result = 1.0;if(n >= 0)

*for(int i = 0 ; i < n ; i++)result = x;elsefor(int i = 0 ; i < -n ; i++)result /= x;return result; }