πŸ“œ ⬆️ ⬇️

Visual C # for beginners. Lecture 6. Functions

Hello!


I apologize for the long absence of video course lectures. There were several reasons why it was not possible to record, but now, I think that I will continue to record the video more or less stable.

And now on the topic


In this lecture you will learn about the functions, parameters of functions, the scope of variables and some other useful things that will undoubtedly be very useful in the future. The lecture was even longer than the previous ones, so, starting with the next one, I will break it into 2 parts, because there is really a lot of material!

Links to previous lectures

Lecture 1. Introduction
Lecture 2. Hello, World! and familiarity with Visual C # Express 2010
Lecture 3. Variables and Expressions
Lecture 4. Conditions and cycles
Lecture 5. Type conversion. Enumerations, structures, arrays

Answers to previous homework:

1. Which of the operations can not be performed implicitly:
c, d
bool in string
byte to float
')
2. Create a code based on the short type for listing a color containing 4 different colors. Can such an enumeration be based on byte?
Answer: To create an enumeration, you can use an example from 4 lectures. You can use the type byte.

3. Will the following code be compiled and why?
string [] blab = new string [5]
string [5] = 5th string ;

* This source code was highlighted with Source Code Highlighter .

Answer: no, since the value must be assigned to the array (blab [5] = "5th string"), the string value must be in quotes, and the index is outside the array.

Enjoy watching!


Also as an addition - a video that I decided not to post in a separate post:
Visual C # for beginners - Code Upgrade # 1


Source codes from lectures

Laboratory work β„–1. Search for the largest element in the array
static int MaxValue( int [] intArray)
{
int maxVal = intArray[0];

for ( int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
maxVal = intArray[i];
}
return maxVal;
}

static void Main( string [] args)
{
int [] myArray = {1, 8, 3, 6, 2, 5, 9, 3, 0, 2};
int maxVal = MaxValue(myArray);
Console .WriteLine( "The maximum value in myArray is {0}" , maxVal);
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work β„–2. Using an array of parameters
static int SumVals( params int [] vals)
{
int sum = 0;
foreach ( int val in vals)
{
sum += val;
}
return sum;
}

static void Main( string [] args)
{
int sum = SumVals(1, 5, 2, 9, 8);
Console .WriteLine( "Summed values = {0}" , sum);
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work number 3. Passing parameter by reference
static void ShowDouble( ref int val)
{
val *= 2;
Console .WriteLine( "Doubled value is {0}" , val);
}

static void Main( string [] args)
{
int myNumber = 5;
Console .WriteLine( "myNumber is {0}" , myNumber);
ShowDouble( ref myNumber);
Console .WriteLine( "myNumber is {0}" , myNumber);
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work β„–4. Passing parameter by reference
static void ShowDouble( ref int val)
{
val *= 2;
Console .WriteLine( "Doubled value is {0}" , val);
}

static void Main( string [] args)
{
int myNumber = 5;
Console .WriteLine( "myNumber is {0}" , myNumber);
ShowDouble( ref myNumber);
Console .WriteLine( "myNumber is {0}" , myNumber);
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work β„–5. Output parameters
static int MaxValue( int [] intArray, out int maxIndex)
{
int maxVal = intArray[0];
maxIndex = 0;
for ( int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
{
maxVal = intArray[i];
maxIndex = i;
}
}
return maxVal;
}

static void Main( string [] args)
{
int [] myArray = {1, 8, 3, 6, 2, 5, 9, 3, 0, 2};
int maxIndex;
Console .WriteLine( "Maximum value in myArray is {0}" , MaxValue(myArray, out maxIndex));
Console .WriteLine( "First index with max value in element {0}" , maxIndex + 1);
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work β„–6. Variable scope
static string myString;

static void Write()
{
string myString = "String defined in Write()" ;
Console .WriteLine( "Now in Write()" );
Console .WriteLine( "Local string myString = {0}" , myString);
Console .WriteLine( "Global string myString = {0}" , Program.myString);
}

static void Main( string [] args)
{
string myString = "String defined in Main()" ;
Program.myString = "Global string" ;
Write();
Console .WriteLine( "\nNow in Main()" );
Console .WriteLine( "Local string = {0}" , myString);
Console .WriteLine( "Global string myString = {0}" , Program.myString);
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work number 7. Command line arguments
static void Main( string [] args)
{
Console .WriteLine( "{0} command line arguments were specified:" , args.Length);
foreach ( string arg in args)
{
Console .WriteLine(arg);
Console .ReadKey();
}
}


* This source code was highlighted with Source Code Highlighter .


Lab β„–8. Using functions in structures
struct customerName
{
public string firstName, lastName;
public string Name()
{
return firstName + " " + lastName;
}
}

static void Main( string [] args)
{
customerName myCustomer;
myCustomer.firstName = "John" ;
myCustomer.lastName = "Franklin" ;
Console .WriteLine(myCustomer.Name());
}


* This source code was highlighted with Source Code Highlighter .


Laboratory work β„–9. Function overload
static int MaxValue( int [] intArray)
{
int maxVal = intArray[0];

for ( int i = 1; i < intArray.Length; i++)
{
if (intArray[i] > maxVal)
maxVal = intArray[i];
}
return maxVal;
}

static double MaxValue( double [] doubleArray)
{
double maxVal = doubleArray[0];

for ( int i = 1; i < doubleArray.Length; i++)
{
if (doubleArray[i] > maxVal)
maxVal = doubleArray[i];
}
return maxVal;
}

static void Main( string [] args)
{
int [] myIntArray = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 };
double [] myDoubleArray = { 1.3, 8.6, 3.8, 6.1, 2.6, 5.8, 9.6, 3.4, 0.3, 2.1 };
int intVal = MaxValue(myIntArray);
double doubleVal = MaxValue(myDoubleArray);
Console .WriteLine( "The maximum value in myIntArray is {0}" , intVal);
Console .WriteLine( "The maximum value in myDoubleArray is {0}" , doubleVal);
}


* This source code was highlighted with Source Code Highlighter .


Lab β„–10. Delegates
delegate double ProcessDelegate( double param1, double param2);
static double Multiply( double param1, double param2)
{
return param1 * param2;
}
static double Divide( double param1, double param2)
{
return param1/param2;
}

static void Main( string [] args)
{
ProcessDelegate process;
Console .WriteLine( "Enter 2 numbers separated with comma:" );
string input = Console .ReadLine();
int commaPos = input.IndexOf( ',' );
double param1 = Convert .ToDouble(input.Substring(0, commaPos));
double param2 = Convert .ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1));
Console .WriteLine( "Enter M to multiply or D to divide:" );
input = Console .ReadLine();
process = input == "M" ? new ProcessDelegate(Multiply) : new ProcessDelegate(Divide);
Console .WriteLine( "Result: {0}" , process(param1, param2));
}


* This source code was highlighted with Source Code Highlighter .


Exercises for independent work

1. The two functions below contain errors. Call them
static bool Write()
{
Console .WriteLine( "Text output from function." );
}
static void myFunction( string label, params int [] args, bool showLabel)
{
if (showLabel)
Console .WriteLine(label);
foreach ( int i in args)
{
Console .WriteLine( "{0}" , i);
}
}


* This source code was highlighted with Source Code Highlighter .


2. Write an application that uses two command line arguments to put the values ​​into a string and an integer variable, respectively, and then reflect those values ​​on the screen.

3. Modify the structure so that it includes a function that returns the total price of the order:
struct order
{
public string itemName;
public int unitCount;
public double unitCost;
}


* This source code was highlighted with Source Code Highlighter .


Thanks for attention! Subscribe to my Vimeo channel and the official VKontakte page to be the first to know about the release of a new video.

Source: https://habr.com/ru/post/115621/


All Articles