-
Notifications
You must be signed in to change notification settings - Fork 540
Functions
Function is a set of instructions. A function is an action. Therefore a verb should be used when naming a function:
Help(person)
Sum(a,b)
MoveTo(location)
Exit()
Console.WriteLine(message)
Are some examples for good function names.
We already know that a function can take parameters, return a value or just do something with the input.
Function Help(person)
takes person
as a parameter.
Function Exit()
takes no parameters and does not return any result.
We are already used to calling functions such as Console.WriteLine(message)
and Console.ReadLine()
.
But how do we write our own function?
Can be public or private. If no access modifier is specified, a function is private.
Only public functions can be accessed outside of a class they belong to.
There are other access modifiers, but about that in later chapters.
Our function: public
Every function needs to return to the context it was called from. Some functions carry a value with them when returning, others don't.
A function which carries no value for return has a return type of void. Other than void, the same types, just like for variables, apply.
Our function: public int
In order to call a function, we need to give it a name. A name needs to start with an alphabetical character.
In 2nd+ symbol, it can include a digit, but no special characters are allowed.
Our function: public int Prompt
Parameter is a variable that goes into the function. A function can have none or many (1 or more) parameters.
Parameter can be a variable or constant value. They are passed right after a function name between ().
Declaring a function, just like declaring variables, requires a type of parameter (if any) to be specified.
When a parameter gets passed, a copy of it is made. Thus, changing a paremter within a function keeps the parameter value the same after it leaves the function.
Our function: public int Prompt(string message)
Every function is inside its' own scope. Scope in C# is all that is between {}
.
Whatever variable is inside a scope does not exist outside of it.
Our function:
public int Prompt(string message)
{
Console.Write("Please enter a number:");
var number = Console.ReadLine();
return int.Parse(number);
}
A function cannot just be defined anywhere. In C#, every function needs to be a part of a class.
A function that can be called without an object (about objects in chapter 2) created needs to be static.
In a nutshell, consider static to be the same as global.
As mentioned before, a function has to return to the context it was called from, with or without a value. This kind of function semantics gives us neat options for design. Ideally, if you can return early from a function- you should. The less we care about in a function, the better. The smaller the scope, the better. But how do we achieve that? Consider the following:
public double CalculateBmi(double weight, double heightInMeters)
{
if(weight > 0 && height > 0)
{
return weight / (heightInMeters*heightInMeters*);
}
else
{
return -1;
}
}
This function uses an if statement (don't pay too much attention to if statement, because it's a focus of the next lesson) to see if the weight and height valid and only then calculate BMI. However, is the else statement really needed?
public double CalculateBmi(double weight, double heightInMeters)
{
if(weight > 0 && height > 0)
{
return weight / (heightInMeters*heightInMeters*);
}
return -1;
}
The above has the same outcome. And we can improve it further:
public double CalculateBmi(double weight, double heightInMeters)
{
if(weight <= 0 || height <= 0) return -1;
return weight / (heightInMeters*heightInMeters*);
}
We inverted the if statement so that we can return invalid result early and removed the scope of BMI calculation, making it even more easy to understand. Again, why is this all possible? Return statement returns to the calling context. In other words- it terminates the function. Therefore it does not matter what you have in your function after the return statement- it will not be executed if return was hit.
Two or more functions with the same name can exist. However, with one condition- they need to have different parameters. For example:
public static void Print(int number)...
public static void Print(double number)...
public static void Print(decimal number)...
We have 3 functions with the same name, but different parameter types. Such function declaration is valid, because each function differs in input type. Such way of declaring functions is called functions overloading. This means that I can print a number which is int, double or decimal. Note that return type or different parameter names have no influence in function overloading and just changing those, will result in compilation errors.
Chapter 1: Fundamentals
Chapter 2: Object Oriented Programming (to be started 2/19/2020)
Chapter 3: Intermediate C# (Q2)
Chapter 4: Testing (Q2)
Chapter 5: SOLID (Q2)
Chapter 6: Desktop UI (Q2)
Chapter 7: Design Patterns (Q3)
Chapter 8: EF and enterprise patterns (Q3)
Chapter 9: Async programming (Q3)
Chapter 10: Web Api (Q3)
Chapter 11: Security (Q4)