2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								title: Functions
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								---
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Functions
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 18:24:48 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Syntax
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Functions in Elixir are defined using the def/2 macro:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```elixir
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								def name(param1, param2) do
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   # Do stuff
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								end
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 18:24:48 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Private functions use the defp/2 macro:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```elixir
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								defp name(param1, param2) do
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  # Do stuff
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								end
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 18:24:48 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Returning
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Functions in Elixir do not use a return statement. Instead, they take the last expression (no matter how deeply nested in the function) and return that.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```elixir
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								def add(x, y) do
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  x + y
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								end
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								def abs(x) do
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if x <  0  do 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    -x
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   else
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    x
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								   end
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								end
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2018-10-12 15:37:13 -04:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### More Information:
  
						 
					
						
							
								
									
										
										
										
											2019-06-24 18:24:48 -04:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								+ < a  href = "https://elixir-lang.org/getting-started/modules-and-functions.html" > Official Module and Function Guide< / a >