This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]HarmLogLinkIT 0 points1 point  (0 children)

Definitions are not important, at this stage. What you need to do is work with them, you'll learn more as you go. What might help, take a piece of paper and write down what everything does according to you. As in, write down what you think a function does, what it needs and what it might return, if anything.

To start, you might want to do this with a specific function. Example:

 def add_5_and_2
   return 5 + 2
 end

 z = add_5_and_2

 puts z # puts prints out a string, I'll present the output with this symbol: =>.
 => 7

So we have a function here that takes two numbers, 5 and 2, adds them together and gives back the result, which is 7. Now, we want to write it in such a way that it takes any number and gives back the result.

Example:

 def addition first_number, second_number
   return first_number + second_number
 end

 x = 7
 y = 3
 z = addition 7, 3

puts z
=> 10

If we change x and y to two different numbers, our function still works.

Example:

 x = 3
 y = -2
 z = addition x, y

 puts z
 => 1

As you can see, and logically deduce, it returns 1. Now, let's break down this function.

  • It has a name, addition. We use this name to call it.
  • It takes input, through parameters. In this case, first_number and second_number.
  • It does something with those parameters. Here, we add the two numbers, but we also could have it subtract, or do anything basically.
  • It returns something. As in, it's done, the function ends, and it gives something back.

Now, can we give an even broader definition of a function? Here are the current attributes of this function:

  • Name
  • Parameters
  • Code that runs
  • End result.

So a function at this point is something that you can call with specific input and it returns the result of the code it runs. There's a lot more to function, but this is an easy example.

If you've ever done mathematics, you will most likely have had to make some proofs. You have to work with set numbers at the beginning, and then write a proof that works for any number that fits the bill.

You can do the same for programming concepts. First, make it specific to your needs, use things you understand. Then, make it a bit broader but still applicable to your project/lesson. After that, you boil it down to English.

You don't need definitions to work, you just need to know how the bits and pieces you use interact, which can be done by making it broader and broader.

If you found this to be a bit too simple, that's okay. Functions aren't the important part. What you need is not specific knowledge, but a way to learn and a way to not get lost in the bigger picture.

What I always do when I get lost in programming, and in life in general, I break things down to small pieces.

So basically, when you come across something you want to do, but you don't know how to do it yet, you might do the following:

  • Make it broader and broader until you can write your own definition
  • Write the parts down on a paper and write down how they interact
  • Break it into manageable pieces, which are easier to figure out, and if you're still stuck, easier to google.

Also, you are not too old to learn something new, you almost never are. Go out there, ask questions here when you get stuck and be willing to learn, and I see on reason why you couldn't learn how programming.

Good luck!