آموزش برنامه نویسی با سی شارپ C# - قسمت 8 – فانکشِن ها (Functions)
زمان مطالعه: 2 دقیقه
زمان ویدیو:
10m59s
لینک یوتیوب
لینک آپارات

What is function?
datatype Name(Parameters ...)
{
// process
// return
return output;
}
bool isDead = false;
float health = 100f;
int hits = 0;
float TakeDamage(float amount = 33f)
{
hits++;
health -= amount;
if (health <= 0)
{
health = 0;
isDead = true;
}
PrintPlayerStats();
return health;
}
void PrintPlayerStats()
{
Console.WriteLine($"Health: {health} | Dead: {isDead} | Hits: {hits}.");
}
PrintPlayerStats();
TakeDamage();
TakeDamage(9);
TakeDamage(3);
TakeDamage();
TakeDamage();
float Divide(int first, int second)
{
return (float)first / second;
}
Console.WriteLine(Divide(9, 3));
Console.WriteLine(Divide(3, 9));
void PrintPlayerStats()
{
Console.WriteLine($"Health: {health} | Dead: {isDead} | Hits: {hits}.");
}
float Divide(int first, int second)
{
return (float)first / second;
}
Console.WriteLine(Divide(9, 3));
Console.WriteLine(Divide(3, 9));
void Shoot()
{
if (isDead)
{
return;
}
// Shoot logic
}
void Shoot()
{
if (!isDead)
{
// Shoot logic
}
}
int Add(int a, int b)
{
int result = a + b;
return result;
}
int threePlusSix = Add(3, 6);
void PrintPlayerStats()
{
Console.WriteLine($"Health: {health} | Dead: {isDead} | Hits: {hits}.");
}
PrintPlayerStats(); // Output: Health: 92.8 | Dead: False | Hits: 2.
void PrintHello(string playerName)
{
Console.WriteLine($"Hello my friend, {playerName}!");
}
PrintHello("GameDevFarsi"); // Output: Hello my friend, GameDevFarsi!
bool isDead = false;
void Shoot()
{
if (isDead)
{
Console.WriteLine("Can't shoot, player is dead");
return;
}
Console.WriteLine("here");
// Shoot logic
}
Shoot();
float TakeDamage(float amount)
{
hits++;
health -= amount;
if (health <= 0)
{
health = 0;
isDead = true;
}
PrintPlayerStats();
return health;
}
void PrintPlayerStats()
{
Console.WriteLine($"Health: {health} | Dead: {isDead} | Hits: {hits}.");
}
TakeDamage(enemyDamage);
TakeDamage(enemyDamage);
TakeDamage(enemyDamage * 30);