Docsity

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

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; }