The Student Room Group

How do I go about writing a function/programming?

Different programming languages such as c#, c++, java, javascript, c
all have different syntax's and python obviously is important with the lining of the code.

However as you all know, the concepts of programming is the same.

How do I go about writing a function?

If I wish to write a function to check destination on a game

Function checkDestination {

when.buttonPaused;
loadmap();
check.location();

}

I know for some function you will need maps, sets such as checking product list from a product class or checking the name of the players for the team class, I believe I am on the right track.

Or could you give an example of a function yourself to help me?

Do you just use logic in the sense with the function I have written? Even though I could be wrong with how I written the function, after all programming is about problem solving.
You can (and often should) use a function for pretty much anything really. A function is a really good way to help write your code in a way which helps to add some "semantic" benefits to your code -- i.e. writing code using functions lets you provide more meaning and clarity by splitting things up into logically-sensible pieces -- that usually makes everything so much easier compared with writing everything in one really huge, long 'script' with loads of if/else/for/while/etc and everything in one big "melting pot" of code.

But also, functions are really good for writing some generalised code that you can also re-use in multiple places -- if the code is in a function, that means you don't need to copy+paste it to use that code. You can just call the function with different inputs instead.

Just remember, that a function is nothing more than just a block of code with a name.. that's it. splitting your code down into functions is nearly always a good idea; sometimes even just short functions with only a couple of lines of code can be usefu or help make your code easier to work with, easier to understand, etc.

As a rule of thumb, I think it's good for a function to do one thing and do it well. For example, each of these things could all be good candidates for individual functions:
- Read from the user
- Display some output
- Do a calculation on some items of data and get the result value
- Read from a file and turn it into a list of data
- Write an item or list of data into a file
- Update a database
- query a database and get the results as a list of data

Your pseudocode example of a function seems fine to me, although I'm not sure what you mean by "when" -- the term 'when' sounds like event-driven programming rather than procedural programming. (Which is a different programming paradigm/style, although that uses functions, but in a different way).

But with procedural programming, you could certainly write a function which repeatedly checks some condition over and over until that condition is true.

for example, a 'read valid age' function (pseudocode):
FUNCTION readValidAge()
SET isAgeValid AS boolean = FALSE

WHILE (NOT isAgeValid) DO
age = read_input("Please enter your age: ")

IF (age IS LESS THAN 99) AND (age IS GREATER THAN 0) THEN
SET isAgeValid = TRUE
END IF
END WHILE

RETURN age

END FUNCTION
(edited 4 years ago)
Reply 2
Original post by winterscoming
You can (and often should) use a function for pretty much anything really. A function is a really good way to help write your code in a way which helps to add some "semantic" benefits to your code -- i.e. writing code using functions lets you provide more meaning and clarity by splitting things up into logically-sensible pieces -- that usually makes everything so much easier compared with writing everything in one really huge, long 'script' with loads of if/else/for/while/etc and everything in one big "melting pot" of code.

But also, functions are really good for writing some generalised code that you can also re-use in multiple places -- if the code is in a function, that means you don't need to copy+paste it to use that code. You can just call the function with different inputs instead.

Just remember, that a function is nothing more than just a block of code with a name.. that's it. splitting your code down into functions is nearly always a good idea; sometimes even just short functions with only a couple of lines of code can be usefu or help make your code easier to work with, easier to understand, etc.

As a rule of thumb, I think it's good for a function to do one thing and do it well. For example, each of these things could all be good candidates for individual functions:
- Read from the user
- Display some output
- Do a calculation on some items of data and get the result value
- Read from a file and turn it into a list of data
- Write an item or list of data into a file
- Update a database
- query a database and get the results as a list of data

Your pseudocode example of a function seems fine to me, although I'm not sure what you mean by "when" -- the term 'when' sounds like event-driven programming rather than procedural programming. (Which is a different programming paradigm/style, although that uses functions, but in a different way).

But with procedural programming, you could certainly write a function which repeatedly checks some condition over and over until that condition is true.

for example, a 'read valid age' function (pseudocode):
FUNCTION readValidAge()
SET isAgeValid AS boolean = FALSE

WHILE (NOT isAgeValid) DO
age = read_input("Please enter your age: ")

IF (age IS LESS THAN 99) AND (age IS GREATER THAN 0) THEN
SET isAgeValid = TRUE
END IF
END WHILE

RETURN age

END FUNCTION


Excellent post.

Thank you very much :smile:.

Quick Reply

Latest

Trending

Trending