Você está na página 1de 24

Functions

Not writing function is like being a


flatlander
How so?

● Imagine writing an entire application with just the Main function.


● Big applications like games are made out of thousands of lines of codes. It will be hell
trying to read these.
● Do you know that we can go beyond static void Main()?
● Do you know that we can write our own function like Console.WriteLine()? If you’ll
notice, it does a lot under the hood but you only need to write one code.
● Writing your own functions opens up a new degree of dimension in coding.
What is a function?

● A small program that can take an input, process the input, and outputs a result.
● Encapsulates a piece of code that can be used by other code. Reduces the repetition in
code.
● Breaks up the program into smaller program or manageable pieces. It is inefficient to
write your code in one go. A project can span for months or even years.
● Functions are mostly actions that you call on demand. This is why functions are named
using verb form.
Parts of Functions
Parameters - input data for
Access Modifier - functions. Separated by
Static - Always write You don’t have to comma for multiple
this for now. We’ll get write this for now. All parameters.
back to this later in the functions are private
course. by default.

Return Type - Data


type of the output.
You can choose not
to output anything
by setting it to void.

Function Body - This is


what the function is doing
(process).
Function with Return Type

● Remember Input-Process-Output. Return is the


output part of the equation.
● Since output is data, it has a data type.
● Inside the function body, you are required to
write a return statement.
● When returning something, you have to include
a value or a variable that is of the same data
type. An int function is expected to output an int
value.
It won’t let you compile if you don’t return anything.
Void Function

There are functions that don’t really have


an output.

In this case, we set the return type to void.


Also, we don’t need to return anything
(obviously).
How is this reusable?

See how we’re just using one line of code to


the entire process of adding.

Our function is very simple right now. There


are functions that takes more than 10 lines of
code. Imagine repeating that code over and
over again.

Notice how I’m using the result of the function


directly (not stored in a variable). Again, you
can always store the variable if you’re gonna
need it later.
Try it yourself!

● Write a function that computes for the area of a


circle given a radius.
● Let the user input the value for radius.
● We need a more precise output. Specifically, a
number with decimal.
● Print the resulting area.
● Don’t forget to test if the output is correct.
Function Overloading

● Allows you to write functions with the same name but with different set of parameters.
● This creates different ways to use the function through parameters.
● The compiler can differentiate the functions by reading their function signature.
● Function signature is defined by:
○ Function name
○ Parameter list
○ Return type is NOT included. Be careful.
Function Overloading
Benefits of using Functions

● Code Organization - Break up large complex tasks into smaller and manageable tasks.
○ DO NOT PUT EVERYTHING IN MAIN()
● Code Reuse - Once you’ve written the function, you can call it from anywhere.
○ AVOID COPY PASTING CODE
● Dynamic Input - With the same function, we can supply it with different values for
input.
How do I name my functions?

● Mostly, in verb form


● English
● Name the function based on what it does. If you
cannot describe the function with simple words,
you are probably doing too much in the function.
Break it down into smaller function if possible.
DON’T DO
THIS!
Variable Scope
Variable Scope

● Refers to which variables are accessible in a specific location of code


● Variables can only be either local or global relative to a code block { }
● Variable scope depends on which code block { } you are in right now.
● Global variables are accessible in any local block
under it.
● Variable is local if it’s in the same block where it’s
declared.
● Local variables cannot be accessed from other
local blocks in the same level.
Out of Scope

● When a variable is out of scope, that


variable is lost forever. No part of the
code can access it anymore.
● All local variables within a block goes
out of scope when the block ends.
● This is the reason why local variables
cannot be accessed outside of the
block where they are defined.
Using Arrays in Functions
Using Native Array as parameter
Using Native Array as return type
Early Return

● When return is invoked inside a


function, it will skip all the code below
and exit out of the function
immediately.
● This is good for handling invalid
parameters.
● You have to be pessimistic when
coding. Meaning, always think of what
will make the function fail.
● Early return also lessens the nesting of
if and loop blocks. Early return is more
readable.

Você também pode gostar