try out some hands-on exercises with examples. But before the exercises,

here are quite a few more examples and notes. In this post, let us now

examine what happens if we write and call a regular function (those with

return type other than void).

First, let's consider a famous calculus problem: finding derivatives of

power functions. A derivative is the rate of change of a function over time,

usually denoted as F(x) prime or dy/dx. In our example, we want to calculate

the derivative of a power function (x^y) with the following algorithm, or

technically called specifications:

We want to return an integer value, so the return type would be an int. We

need two parameters: the function base, or f(x), and the exponent (^y),

denoted as base and power, respectively. We want to simply multiply the base

with power then return the multiple. For instance, if we want to find the

derivative of x^4, we would put 1 as base and 4 and power, then multiply

these two and return the result (4). The actual derivative is 4x^3.

Can you picture a possible problem? If you can picture it, I'll consider you

a genius (and perhaps add you as a Facebook friend...). But I will not touch

the problem yet, since the exercise here is to writing a functional

"function body." Then can do you have any idea on how you can help me

transform this English specs into C++?

The answer: the function signature, or the identifier that we use from a

host program to call this particular function goes something like this:

int derivative(int base, int power)

// The function signature with int as return type with arguments separated

by a comma.

{

int multiple; // The multiple of the base and power.

multiple = base * power; // The actual operation.

return (multiple);

}

Pay close attention to the "return (multiple);". This is the syntax that we

use when we are dealing with a function like this. in order for the host

program to process what we get here, the multiple must be used within a

method such as part of an output statement, or it needs to be assigned to

another integer variable. We'll see both examples in just a second.

Note: The derivative formula for power functions: multiply the base and

power and put this in front of the X, then bring the power down by one.

So, a more complete program which calculates a derivative is:

#include <iostream>

#include <string>

using namespace std;

// A function to calculate derivatives of power functions.

int derivative(int base, int power)

{

int multiple = base * power;

return (multiple);

}

// The actual program:

int main()

{

// a simple derivative calculator.

cout << "Welcome to Joslee's derivative calculator." << endl;

int func_base;

int func_power;

cout << "Please enter the base of your function:" << endl;

cin >> func_base;

cout << "Please enter the exponent of this function:" << endl;

cin >> func_power;

int dydx = derivative(func_base, func_power);

cout << "dy/dx " << func_base << "X^" << func_power << " = " dydx << "X^" <<

func_power-1 << endl;

return (0);

}

A bit complicated... But I'm sure you'll understand it better if I try this

program out:

Computer: Welcome to Joslee's derivative calculator.

Computer: Enter the function base:

Me: types "5".

Computer: Enter the exponent:

Me: types "3".

Computer: dy/dx 5X^3 = 15X^2.

Yes, and that's a bit better than writing the whole derivative operation in

the main() code.

As an added bonus, we can add conditional statements in our functions -

which allows me to answer the forseen problem of a user entering zero or 1

at the exponent prompt. To help us with this mess, we can add a useful

conditional statement (perhaps an if statement or two) to test whether the

entered value is zero or not. I cannot test for input of 1, as this

complicates matters worse (we need to create another function which is

called by the derivative function to sort this one out); right now, I don't

have enough time and space to try it out yet - perhaps next time. But the

point here was that we can do anything with a function, just like we can do

anything in main() (after all, main() is a function - a special one, after

all).

Hope this gives you some picture of some basics in functions. There are more

things to be talked about, but I'll leave it up to more posts to do that. As

for what's ahead, I'm not even half way through - I still need to talk about

member functions, passing by value versus by reference and so on...

note: A member function is an action (or a function) that can be done with

that particular data type (technically an object). For instance, if we have

a vector and if we want to add new values, we can use push_back member

function under vector object (data) to do that. A member function has a form

of data.function where "data" is the actual data type that we are working

in, and the "function" is the member function of that data. So, in tech

language, the push_back is a member function (or a member) of vector object.

We'll come back to the notion of member functions much later when we talk

about designing our own data types.

P.S. Actually, under void functions, you can have a return statement like

return;

I think that's enough for now. I'll come back with more practices with

functions. Again, if the content was not simple (or incorrect) for people to

understand, I apologize. As always, feel free to comment this post and

others.

// JL P.S. Most of the notes come from CS010 class and various books.

## No comments:

## Post a Comment