Learning Programming #1.4 The Basics: Functions

in programming •  6 years ago 

Functions are not really necessary in programming, but they are a great way of making your code more readable for humans which is very important because you want to be able to understand your own code right?
In the last post I introduced conditions and loops which are quite essential for programming.

Functions usually consist of four to five parts:

  1. The function name: Like variables each function has a name so you can call them apart.
  2. The return value. This is the type of the variable you will get every time you call this function. There are also functions without a return value. Those are defined usually with void instead of the usual return value. Python again needs no specification of the return value and uses the type of the variable that is used in the return statement.
  3. The function arguments. Those are variables that the function needs to work. You can have as many arguments as you want if you define a function. When calling a function you have to match the number and type of variables that were defined in the function in the order they were defined. You can even have 0 arguments if you want. In that case you just leave the point, were the arguments are usually found, empty.
  4. The function body. This is the same as the body of a condition or loop: Just a place where the you put the code that you want to be executed when the function is called.
  5. If you have a return value specified then you need to return a variable of the type in your function. This is done with the return statement:
    java: return variableName;
    python: return variableName
    As you probably noted so far python and java are not too different. Python just has a few less syntax specifiers.

Here is a small example how a function will look like in java:
Screenshot from 2019-05-06 15-06-22.png

You could also make the functions static if you wouldn't want to create a new Object and call them directly(without objectname+".").
You can create functions not inside of other functions bodys(unless you have some special case) and in java you can create functions like most other things only inside of a class.

You can also call a function from within itself:

void selfCall() {
    selfCall();
}

If this function would be called once your program would get stuck trying to call one function infinitely often. Usually you will get a StackOverflow error there.
In some cases it is usefull to have function call itself. I will come back to this when talking about recursive algorithms.

The functions used in programming can be pretty similar to those you might know from maths:
f(x) = x² can be written in java as:

int f(int x) {
    return x*x;
}

You can do the same with functions of multiple variables like:
g(x, a) = a*x²:

int g(int x, int a) {
    return a*x*x;
}

There are also conditional functions in maths like the value function which can also be used in programming:
value(x) = x for x ≤ 0
value(x) = -x for x < 0

int value(int x) {
    if(x >= 0) {
        return x;
    }
    return -x;
}

or:

int value(int x) {
    int result;
    if(x >= 0) {
        result = x;
    }
    else {
        result = -x;
    }
    return result;
}

Task

Write a java function for the signum function:
sgn(x) = 0 for x = 0
sgn(x) = 1 for x > 0
sgn(x) = -1 for x < 0

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @quantumdeveloper! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You got your First payout

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemitBoard - Witness Update
SteemitBoard to support the german speaking community meetups
Vote for @Steemitboard as a witness to get one more award and increased upvotes!