Why Functions?
If you are not that rare someone from the WET (We Enjoy Typing) camp you must be someone who is happy to follow the DRY (Don't Repeat Yourself) principle. Functions facilitate code reuse and are thus compatible with DRY.
If you're already familiar with the basic of functions you can read the next articles of the series:
Example# 1
Output of example# 1
Example# 2
Play here
You can copy the below code, it's not an image:
Output of example# 2
hello from a function without a parameter!
What's the difference between Example#1 and Example# 2
Example# 1 has a function named test() that has no parameters. Example# 2 has two functions, one with parameter and another without any parameter.
What's the difference between a Parameter and an Argument?
A parameter is the variable which is part of the function's signature. See example# 2:
Here, the variable p is a parameter.
Whereas an argument is the real value/data that is passed to the function when calling it. See example# 2,:
As we tend to learn quick by examples here are a few more examples of function:
Addition of Two Numbers
Example# 3
Play
Output
9
Function to find Odd or Even Numbers using Modulo Operator
Example# 4
Play here
Output Example# 4
Example# 5
Function to find Odd or Even Numbers using Bitwise Operator
Play with the code
You can copy the below code, it's not an image:
The output of example# 5 is same as example# 4.
Additional Information
Do you want to know what's the precise difference between a Function and a Method? Read this post Go Methods to understand it.
Bitwise Operator to find Even or Odd Number
With reference to the code example# 5 many of you who are new to programming may not have understood the logic behind the code. It's a bit tricky for beginners but it is simple. Let us try to understand:
Important Note:
We've used Bitwise & operator as follows:
Here's a good codecademy beginners level tutorial [FREE] if you want to learn more about bitwise operators.
Did you like this? Please share and let me know if I can improve this to make it more beginner friendly.
Go Functions has same use as functions in any other language. They serve as basic building blocks of a software program. You can break a big problem into various smaller chunks i.e. functions which can be called wherever required i.e. you can avoid redundant code.
If you're already familiar with the basic of functions you can read the next articles of the series:
Let us try a simple function. In the following example we've defined a function named test() which is getting called in the main() function. You can play with the code here
Example# 1
Output of example# 1
Example# 2
Play here
You can copy the below code, it's not an image:
package main import "fmt" func main() { funcWithoutParam()//Calling a function that has no parameter //r is an argument, is passed to funcWithParam r := "hello from a function with a parameter!" fmt.Println(funcWithParam(r)) } //Function without a parameter func funcWithoutParam() { fmt.Println("hello from a function without a parameter!") } //Function with a parameter func funcWithParam(p string) string { return p }
hello from a function without a parameter!
hello from a function with a parameter!
What's the difference between Example#1 and Example# 2
Example# 1 has a function named test() that has no parameters. Example# 2 has two functions, one with parameter and another without any parameter.
What's the difference between a Parameter and an Argument?
A parameter is the variable which is part of the function's signature. See example# 2:
func funcWithParam(p string) string
Here, the variable p is a parameter.
Whereas an argument is the real value/data that is passed to the function when calling it. See example# 2,:
r := "some string text"
func funcWithParam(r)
As we tend to learn quick by examples here are a few more examples of function:
Addition of Two Numbers
Example# 3
Play
Output
9
Function to find Odd or Even Numbers using Modulo Operator
Example# 4
Play here
Output Example# 4
Example# 5
Function to find Odd or Even Numbers using Bitwise Operator
Play with the code
You can copy the below code, it's not an image:
package main import "fmt" func isEvenOdd(x int) string { if (x & 1 == 0) { return "even" } else { return "odd" } } func main() { var i int fmt.Println("Enter an Integer") fmt.Scanf("%d", &i) fmt.Println(isEvenOdd(i)) }
Additional Information
Do you want to know what's the precise difference between a Function and a Method? Read this post Go Methods to understand it.
Bitwise Operator to find Even or Odd Number
With reference to the code example# 5 many of you who are new to programming may not have understood the logic behind the code. It's a bit tricky for beginners but it is simple. Let us try to understand:
In natural languages, when we count we use base 10 i.e. to form a number we use any of the digit starting from 0 to 9 (i.e. we have 10 options). We call this system as Decimal Numbers.
Computer DOES NOT understand natural language i.e. Decimal Numbers. It understands only two numbers 0 and 1 or numbers formed by combination of these two numbers. We call this system as Binary Numbers.
Decimal Binary
0 0
1 001
2 010
3 011
4 100
5 101
6 110
7 111
If you notice carefully the binary equivalent of any Odd number (marked in blue font) has 1 at the right most position. The Even numbers (marked in black font) have 0 as the right most position.
Important Note:
Bitwise & returns a one in each bit position for which the corresponding bits of both operands are ones.
We've used Bitwise & operator as follows:
if (x & 1 == 0)
Now let us decipher the above line of code.
Suppose x is 4. Now the system internally translates the above line to:
if (100 & 001 == 0)
As the rightmost digits of the above comparison are 0 and 1, hence the output of a bitwise & operation will be 0, it means the if condition is true. So the function will return "even".
Suppose x is 5. Now the system internally translates the above line to:
if (101 & 001 == 0)
As the rightmost digits of the above comparison are 1 and 1, hence the output of a bitwise & operation will be 1, it means the if condition is false. So the function will return "odd".
Can you alter the if condition by comparing x with 0 or by equating x & 1 with 1?
Here's a good codecademy beginners level tutorial [FREE] if you want to learn more about bitwise operators.
Did you like this? Please share and let me know if I can improve this to make it more beginner friendly.
No comments:
Post a Comment