Hey awesome people! How are you doing today? Are you having fun and enjoying this wonderful gift called life? Did you learn anything today?
You know, there is fun in learning new things, there is enjoyment in learning new things. I hope that you are enjoying as well as learning new things every day. Now, without any further ado let's talk about what I learned today.
Today, I dived into the realm of functions and methods in Go. It was amazing. I learnt about the anonymous function. I learnt about the first-order function and passing these functions as parameters, returning multiple return values. Then I learnt about writing Variadic functions in Golang. These were the new things I learnt about functions in Go.
Now, for today's coding example, I would like to share a simple variadic function code with you. Here is the code example, take a look at it and then we will talk about what it does how it works?
sum function returns the sum of all the numbers that are passed as a parameter. This is a Variadic function and in the function call, we are printing the return value. Now let's talk about the code part of the example.
This function will take an arbitrary number of ints as arguments. It is possible because we used the
... operator before
int. We have a variable named
result to store the sum. We are looping through the
nums and in the loop we are adding each element to the
result variable. When the loop ends, we are returning the
result variable as the return value. In the main function, we are calling the
sum function and the return value is then printed in the main function. It's as simple as that.
Alright! That's all for today. Do follow me for more posts like this. Also, after the completion of the challenge, I will be covering the entire fundamentals of the Go Programming Language in great detail. So be on the lookout for that.
Connect With Me:
Top comments (0)