Posted by : Unknown Sunday 9 June 2013


Functions in programming are program fragments used to segmentate programs ranging from simple spreadsheets/applications to complex multi-threaded programs and games. Functions allow you to compartmentalise and organise your programs logic. They enable you to split your the contents of your application into logical blocks that invoked in sequence.

A function is used to take an input, calculate on input and return a value that in large applications will usually be put forward to a larger function or be used multiple times depending on its role. The beauty of a function is the fact that it can be called an infinite times in your application to do x job at hand.

Functions have roots in lambda calculus and were introduced by Alonzo's Church's calculus in 1930. Lisp was the first programming language to debut functions in its design by John McCarthy in 1958.

Lets take a look at function prototypes. These examples are coded in C++
If calculating a circle...

double circumference(double InputRadius);

There are 3 logical parts to a function prototype the first is return the value which is the first double. The second is the function name to refer back which is circumference. The third is optional and is the function parameters comprised of type and optional name. When there are multiple parameters a comma is used to separate. A function can have multiple parameters but it can only have one return type.

After declaring the prototype you will declare a function definition which consists of a statement block. The function definition houses all calculations relevant to the problem the function is trying to solve. The statement block is identical to housing your first program in int main()
double circumference(double InputRadius)
{
       calculationHere;
}
Once you have stated the function definition you can make a function call. For example:
cout << "Circumference of circle is: " << circumference(Radius)<< endl;
When making a function call you are causing the program to jump to the function statement, calculate and then jump back to where the result will be printed via the cout statement. 

There are more features to functions but this extends the scope of this post.


Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Blogger templates

Labels

Copyright 2013 JOG-AU. Powered by Blogger.

Blogroll

- Copyright © JOG-AU -Metrominimalist- Powered by Blogger