golang tutorial - Golang Function | Go - Functions - golang - go programming language - google go - go language - go program - google language
Golang Function
- A function is a block of code that performs a specific task.
- Suppose, a program related to graphics needs to create a circle and color it depending upon the radius and color from the user. You can create two functions to solve this problem:
- create a circle function
- color function
- Dividing complex problem into small components makes program easy to understand and use.
Types of functions
- Depending on whether a function is defined by the user or already included in go compilers, there are two types of functions in go programming
- There are two types of functions in go programming:
- Standard library functions
- User defined functions
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Standard library functions
- The standard library functions are built-in functions in go programming to handle tasks such as
- The standard library functions are built-in functions in go programming to handle tasks such as
- mathematical computations,
- I/O processing,
- string handling etc.
- These functions are defined in the header file. When you include the header file, these functions are available for use. For example
User-defined functions
- As mentioned earlier, go allow programmers to define functions. Such functions created by the user are called user-defined functions.
- Depending upon the complexity and requirement of the program, you can create as many user-defined functions as you want.
golang , gopro , google go , golang tutorial , google language , go language , go programming language
How user-defined function works?
- Step 1: The execution of a C program begins from the main() function.
- Step 2: When the compiler encounters functionName(); inside the main function, control of the program jumps to
- Step 3: the compiler starts executing the codes inside the user-defined function.
- Step 4: The control of the program jumps to statement next to functionName(); once all the codes inside the function definition are executed.
Sample program for go functions
golang , gopro , google go , golang tutorial , google language , go language , go programming language
Output of the above go language program
Advantages of user-defined function
- The program will be easier to understand, maintain and debug.
- Reusable codes that can be used in other programs
- A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.