Try   HackMD

Functions in programming (IB pseudocode)

This develops the part of "Develop algorithms that include pre-define sub-programmes"

What is a function

In Computer Science there are many names for functions. Sub-programmes, subroutines, methods (that one in Java context) and functions.

I usually use functions unless I'm in Java that I'm more used to "methods" since is the OOP jargon.

A function is to put it simply, a bunch of code that is executed when it's called.

Many of them are distinguished by being a one word (in camelCase) followed by a parenthesis like createInterior() or handleCommunication()

These functions may or may not return something or may or may not have parameters.

Anatomy of a function

Parameters

Remember that in math we have functions like

f(x) ? x in that case would be the parameter. It's something that we sent to the function in order to work.

Parameter or argument

Usually parameter and arguments are synonims but if you want to be te a little bit more precise,

/TO-DO

You can think of chatGPT as a function that has as a paramiter an input text (a string) that is sent online and returns another text.

If you have a function to convert currency (for example Euros to Japanes Yens) you will need to tell that function how many euros you want to convert to Japanese Yens.

Difference between input an parameter. A parameter (or )

Functions with no parameters

We can have functions that doesn't have parameters, such as "shutDown()" since it doesn't require anything for shutting down.

Also in the examples that we have done in c++ with arduino void setup() and void loop() don't have parameters.

Also in the examples of the morse code that we have done, all the functions void morseA() or void morseDot() or void morseDash()

In general if a function doesn't require any instructions from the part of the code that it's calling it it will have no parameters

More than one parameter

Sometimes we may need more than one parameter. For example, if we want to do a sum of 2 numbers, we need these 2 numbers.

Other examples calculateRectangleArea(x,y) would be a function that calculates the rectangle area

In many programming languages you can have indefinite (limited to memory) parameters but we usually stick to the strictly necessary for being sane. Usually a function with 4 or more parameters it has too many parameters.

In Math also we can have functions with more than one parameter since stuff may depend on more of than one variable. Then we can plot the results that we get. For example here you have the graph of the function

f(x,y)=x·y (the area of a rectangle with the sides x and y)
Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

done with this tool

Return

//TO-DO

The return statement

//TO-DO

Declaration, definition and call

//TO-DO

Exercise of declarations

Here I need that you declare at least 3 functions (of whatever context)

I need the signature and you need to specify what would be the parameters and if there is a return or not (and in that case what is the data returned).

At least one with parameters
At least one without parameters
At least one that returns something
At least one that doesn't return anything.

Solutions:

//TO-DO

Exercise of calling functions

//TO-DO

Solutions:

//TO-DO