Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 10

Functions

By: Paul
What is a function

A function is a self-contained and reusable block of


code that performs a specifi c task or set of tasks.
They are a crucial tool for structuring programs,
improving code organization, and promoting code
reuse. They allow programmers to create more
effi cient, modular, and maintainable software.
Characteristics of Functions
Modularity: Functions allow you to break down a program into smaller, more manageable pieces. Each function can
focus on a specific task, making the codebase more organized and easier to understand.

Reusability: Once a function is defined, it can be called multiple times from different parts of the program. This
promotes code reusability and reduces the need to duplicate code.

Abstraction: Functions abstract away the details of their implementation, providing a clear interface for interacting
with them. This helps other parts of the program use the function without needing to understand its internal workings.

Parameterization: Functions can accept inputs known as parameters or arguments. These values can be provided
when the function is called and used within the function's code to customize its behavior.

Return Values: Functions can also produce output, known as return values. A return value is the result of the
function's computation and can be used by the calling code.

Encapsulation: Functions can encapsulate logic and data, controlling the scope of variables and data structures within
the function. This can help prevent unintended interactions with other parts of the program.
Structure of Functions

r etu r n _ t y p e f u n c t i o n _ n a m e ( p a ra m e t e r s )

/ / wr i t e yo u r c o d e h e r e

r et u r n va l u e ;

r etu r n _ t y p e = va r i a b l e r e t u r n ty p e e . g . ( i n t , d o u b l e , fl o at , B o o l e an , vo i d e t c )

f u n c ti o n _ n a m e = w h a t d o yo u wa n t t o c a l l yo u r f u n c ti o n

p a ra m e t e r s = w h a t a d d i t i o n a l i n f o r m a t i o n d o yo u wa n t t o g i ve i t ( var i a b l e s )

r etu r n = t h e va l u e t h e f u n c t i o n wi l l r e t u r n
void paul()

/ / w r i t e yo u r c o d e h e r e

/ / n o t e t h a t a vo i d f u n c t i o n d o e s n o t h a ve a r e t u r n va l u e

int calculate(int a, int b)

int sum = a + b;

return sum;

d o u b l e d o _ s o m e t h i n g ( S t r i n g n a m e , i n t wa i t )

/ / w r i t e yo u r c o d e h e r e

r e t u r n va l u e ;

N o t e : Yo u c a n n o t h a v e a f u n c t i o n i n s i d e a f u n c t i o n
Calling a function

void paul()
{
//write your code here
//note that a void function does not have a return value
}

void loop()
{
paul();

}
Calling a function

int calculate(int a, int b)


{
int sum = a + b;
return sum;
}
void loop()
{
paul();
int total = calculate(5,8);

}
Calling a function

double do_something(String name, int wait)


{
//write your code here
return value;
}
void loop()
{
paul();
int total = calculate(5,8);
double number = do_something(“paul” , 500);

}
Guidelines for functions
TO BE CONTINUED

Function
Naming
Function
Signature

You might also like