help understanding functions

Anything that doesn't fit elsewhere.
Post Reply
JimH
Posts: 54
Joined: Tue Apr 10, 2007 3:36 pm
Location: Duke University

help understanding functions

Post by JimH »

After quite a bit of playing around I think I have a function in which you pass in a list of sections (SectionList) and it creates a set of vectors for recording the membrane potential at each segment for each section in the list. I've spent a lot of time trying to understand procedures and functions and I was hoping to make sure I understood some of the main points.

NOTE: These are my thoughts and not necessarily right ...
1) Objects that are passed into a function must have been defined as objects previously. What about declaration of the type of object?
2) local and localobj prevent variables from showing up in the parent's workspace as well as from using the parent's variable with the same name in the function. Localobj means the objref declaration is not necessary.

Corrections or additions to the points above, as well as code improvement suggestions to the code below are most welcome.

Thanks,
Jim

Code: Select all

obfunc sim_logging__record_node_voltages(){ local cur_seg localobj node_vm_hist, tempRecVec, node_section_list
//sim_logging__record_node_voltages
//
//    node_vm_hist = sim_logging__record_node_voltages(node_section_list)
//
//INPUTS
//---------------------------------------
//node_section_list - (Class SectionList)
//
//OUTPUTS
//node_vm_hist - (Class List) - holds vectors of membrane potentials at each segment

tempRecVec   = new Vector()
node_vm_hist = new List()
node_section_list    = $o1

cur_seg = 0
forsec node_section_list {
	for (x,0) {
			tempRecVec = new Vector()
			tempRecVec.record(&v(x))
			node_vm_hist.append(tempRecVec)
	}
	cur_seg = cur_seg + 1
}

return node_vm_hist
}
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: help understanding functions

Post by ted »

arguments, local, and localobj are all documented in the Programmer's Reference.
The example code you provide looks like it should work. Does it?
JimH
Posts: 54
Joined: Tue Apr 10, 2007 3:36 pm
Location: Duke University

Re: help understanding functions

Post by JimH »

It does in fact work. I was a bit surprised that node_section_list doesn't need to be declared as a SectionList() here. Most of the examples I have seen in the documentation start off with creating a new object inside of a function, as opposed to working with one that has already been created elsewhere, so one of the first lines is usually

<my_object> = new OBJECT()

I had been getting errors which I thought were attributable to not declaring the type of the object, but they usually were just trying to indicate other errors in the function.

The Programmer's Reference is a decent start but is lacking examples. Searching the forum for help on the topic has proven difficult. I think fortunately I am starting to get the hang of it but just wanted to confirm. I think at this point the debugger, or lack there of, is the most challenging aspect when creating a function. In many cases I think it is more useful to consider error messages as indicating that a problem exists, as opposed to indicating what or where the actual problem is.

Thanks,
Jim
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: help understanding functions

Post by ted »

The Programmer's Reference is indeed compact if not taciturn. For good examples of procs and funcs and decent programming style, it is useful to examine articles and other publications about NEURON itself; those are cited here
http://www.neuron.yale.edu/neuron/nrnpubs. Much of that source code is available from ModelDB, and for the most part it is written in a mildly didactic style and is reasonably well commented. Also, every installation of NEURON comes with a library that implements the standard run system and the graphical user interface. The library is implemented in hoc and contains probably hundreds of procs and funcs; they aren't heavily commented, but most are relatively short and easy to "understand" in the sense of being able to discern the underlying pattern. See Secrets of NEURON: the hoc library at the very top of the Forum's Hot tips area http://www.neuron.yale.edu/phpbb/viewforum.php?f=28
Post Reply