Jump to content
Sign in to follow this  
atosniper

Learning res for functions?

Recommended Posts

I'm trying to get my head around functions and their various parts, arguments, parameters etc.

 

Does anybody know of good resources to learn about the programming of functions?

 

I have both the Jerry Ford Programming for beginners book and the Wang dummies one, and both are decent but don't go into enough detail for me about functions. Good ole google search is proving as fruitless.

 

Any better ideas?

Share this post


Link to post
Share on other sites

Sorry. No ideas. Functions themselves are rather simple. Arguments = Parameters (or there abouts)

 

Function bits:

- Name = the name of the function

- Parameters/Arguments = the inputs of the function

- Return type = the type of value returned or outputted by the function

- Function Body = the stuff that does the work

- Visibility = what other functions can use this function

 

The syntax of a typical function is language dependant but the following is pretty standard across curly brace typed languages such as java,c,c++,c# etc

Visibility ReturnType FunctionName(Argument1, Argument2, Argument3)
{
	Function Body
}

A real world example in C#

public int Add(int number1, int number2)
{
	// The body - Adding two numbers together.
	// The result of the operations performed in the body must be of the return type 
   // specified in the function definition/declaration. In this case an int.
	return number1 + number2;
}

Then there's all the statements and operations that go inside a function to get it to do the work you intend. The other bit, the bit that juniors mess up is what and how much to put in a particular function. A function should have one specific thing to do. You can do more by calling functions within functions, in that case the one thing that function does is coordinate the other functions.

 

Also, in a functional language, functions are first class citizens and can be passed in as parameters to other functions. Also, try to keep functions self contained, using parameters to pass in anything the function may need to do it's job. This will help keep you focussed on keeping the function focussed on doing One Thing.

Edited by kikz

Share this post


Link to post
Share on other sites

Wow thanks, that was an awesome reply. I'm glad to see there's still life in these forums yet.

 

I've been having a good look at functions again since I posted and think I have solved my dilemma. I'm trying to learn the code for Amibroker btw, which is apparently based on C. Just to check:

 

If I wrote a function like:

Function SumIt ( f0, f1, f2 )

{

//body in here

}

 

And then called the function, for eg

 

SumIt ( 1.5, 2.5, 4.5 )

 

The numerical values in the call part there would be substituted for the f values and used in whatever the body of the function had defined for them. That's right, isn't it?

Share this post


Link to post
Share on other sites

Yep that's right. You got it! f0, f1, f2 are placeholders that hold whatever value you assign to them when the function is called. In your case f0 = 1.5, f1 = 2.5, f2 = 4.5.

 

It is possible to reassign f0,f1,f2 within the function body.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×