Jump to content
Science Forums

Recommended Posts

Posted

okay so i mainly want to cover the chapter on function basics because there is more to cover. In this chapter they talk about Predefined Functions, Programmer-Defined Functions, and Scope Rules. If you want the sub topics of each section of the chapter tell me and i'll do it.

Posted

Hi Sheldon, welcome to Hypography!

 

Your post is unrelated to the thread you originally put it in, so I've moved it to a new thread.

 

If you'd like help, it would be a good idea to be very specific about what issues you want to discuss and what you'd like to know.

 

An expert knows all the answers - if you ask the right questions, :phones:

Buffy

Posted

Hi Buffy,

I'm glad to see you're here. We need a problem to solve which will require design and analysis involving "Predefined Functions, Programmer-Defined Functions, and Scope Rules" in the object oriented paradigm. Something which can be done in an hour or less.

Posted

it depends on how complex you want to get, but one thing you could do is perhaps run a grocery check simulation. a customer brings a random number of items from 10 to 35, puts them on the counter from lightest to heaviest, and the cashier has to run them over the scanner to check out their price and give a total.

Posted

Good news, everyone, its time for a lesson on functions... yes, yes, i'm quite sure of it. (lets hope this gets a Futurama reference of the month award)

 

Ok, so where to start. Just about every language so far in existence is built around a procedural core, in just about every language one can not only program line by line, but for things that require frequent calling, create and store routines that can then be called to, by that both reusing code and simplifying development, with one section of code to manage for that specific procedure regardless of where in your main program it gets called. These pre-defined and user-defined procedures have a name, and are called functions. They are a sort of a micro-program within a program that have very specific logical tasks. Because of this relationship of a function being a sort of a sub-program they are, in some languages, referred to as "Subs", generally though it means the same thing as a function.

 

Functions can be of two types. Language-defined, predefined is the term you have used here, but i think language-defined is a much better description. These are functions that are defined by a particular language. While they are different from language to language, and sometimes from implementation to implementation, they are there to provide user with a set of things they can do without having to come up with all the logic for them. These functions can include tasks ranging from parsing text, to opening database connections and doing complex mathematical operations.

 

The second type of a function is user-defined. These are functions that you, as a programmer, get to write that suit your own needs. Most of the time, you use these to simplify tasks and reuse the code, usually the user-defined functions will call language-defined ones to make a routine that makes sense logically.

 

Say you were writing code for a robotic arm, a very simplistic one, you have predefined functions that alow you to move the arm along it's rails n'th distance, and another one that alows you to rotate the arm n degrees, third function alows you to raise or lower the arm n distance and finally forth function that crimps the grabbers. That said, your task is to move 3 blocks (same size) from behind the right of the arm to the forefront on the left of the arm. There are some tasks that can be defined in a user-defined function that will simplify coding for you. For example, you know that you will have to move the arm different lengths from right to left and back, so that is not a good idea for a function, but lowering and grabbing the block and raising the arm again can be simplified into one function.

 

 

function raise_block()

{

lower_arm(10)

grip()

raise_arm(10)

}

 

function lower_block()

{

lower_arm(10)

release()

raise_arm(10)

}

 

 

And if you think about it, if the height of lowering crimping/uncrimping the arm are the same, then the lifting and lowering procedures can be combined into one piece of code with a simple decision.

 

 

function lower_raise_block( bool grip)

{

lower_arm(10)

if (grip=true)

{

grip()

}

else

{

release()

}

raise_arm(10)

}

 

 

You see how now in the main code, for every iteration we only call to lower_raise_block function and simply tell it to grip or release the block, we can go further to make it so we can grip blocks of different sizes and use different height platforms, etc, this merely demonstrates how a user-defined function can and often does call language-defined ones in order to perform a repetitive logic task in one or another way (not to be confused with a repetitive task as in a loop).

 

Programming languages split into two types, compiled and interpreted, there are some variants that are both, but generally a language is either compiled or interpreted. You need to remember that there is one big differentiation with those languages when it comes to predefined functions. Most of the time, compiled languages have libraries of predefined functions that you can call on, and depending on the task at hand you may want to use or not use certain ones, this is good when you are limited in size of a program, why include code that is not going to be used anyways, right. Scripting languages (another name for interpreted) usually lack that ability (they do however generally give you an ability to use your own library of user-defined functions) but already come with a wide range of predefined functions, since these languages require an interpreter to execute and are built for speed and simplicity of development, the lack of that ability is not usually a limiting factor with scripts...

 

Lastly, variable scopes. This differs from language to language, but in general, and specifically with C++, there are 2 scopes you deal with, at least for procedural programming, they are local and global.

 

They are fairly straight forward, local variables are variables that define in the scope of a particular function (i.e. from when you define them to the closing bracket of the function). These are the most commonly used variables, infact 99.9% of all the variables you use will be local variables. It's rare to have a need for a global variable as security-wise they may be dangerous to use, there do, though, sometimes arise occasions in which they are the one way out, though...

 

Local variables exist within a scope of a function, remember main (in C++) is a function, thus your variables won't magically travel to external functions that you may call, thus the need for notation of passing variables to a function, and also remember that you can only return ONE result with a return function. Going back to our blocks program (its C++ish syntax)

 

 

bool lower_raise_block( bool grip, int lower)

{

lower_arm(lower)

if (grip == true)

{

grip()

return 1

}

else

{

release()

return 0

}

raise_arm(lower)

}

 

 

this way we can now check if we gripped or released the mechanism (i mean the use of return may not be all that necessary here as we specifically set the state, but it demonstrates another point.) The return function returns the execution of the program back to the place where the code was executed. If you return a value, you can now either get a value back, say if a function calculated the volume of a cone, you would return the answer, or you can not get a value back, if there is no need for it, say you were simply writing your results to a file at the end of program execution, and you passed the function a long string, and all the function did was open and write to a text file, close it, and you were done with execution (granted you didn't care for error checking (and you always should)). The type declaration before the function name signifies the type of a value you are returning (in some languages mainly in scripting languages you don't have to define that).

 

 

-- Alex --

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...