COMP 170 Introduction to Object Oriented Programming Name
COMP 170 – Introduction to Object Oriented Programming <Name of Instructor> Department of Computer Science 1 1
Notes on C# Functions A C# function definition is like writing the instructions for a recipe in a cookbook – nothing happens until you look up the recipe in the cookbook and follow its instructions to make something When C# sees a function definition it simply stores it away until the function is called somewhere else in the program A C# function call is like you taking the cookbook down from a shelf and following the instructions for a particular recipe to make the dish
How Do I Define a C# Function? A C# function definition starts with the special keyword static, followed by the type of its returned value, if any, the name of the function, and parentheses, possibly with comma-separated paired types and item names inside them, like this: static return. Type Function. Name (0 or more items ) these are called parameters The lines immediately following this function header or “signature” line that are inside matching curly braces, { }, are part of the function definition – they are called the function body, and they say what the function will do when it’s called (but the definition is just saved away for now)
How Do I Call a C# Function? Once you have defined a function you call it in your program by writing its name, parentheses, and the same number of commaseparated items that are in the function definition parentheses: Function. Name (0 or more items) these are called arguments You do not write the static keyword, and you do not include the return type or the parameter types; however, the types of the function arguments in the call must match or be “compatible with” the types of the corresponding parameters Once the function finishes executing statements in the function body, program execution continues following the function call (execution “jumps into” the function, performs its statements, and “jumps back” to the point of the call)
C# Function Example Defining and saving away a “Say. Hello” function: static void Say. Hello() // nothing in the parentheses { Console. Write. Line("Hello, world!"); } // note: a “void” return type means no returned value Calling the Say. Hello function: Say. Hello(); // also nothing in the parentheses This prints Hello, world! on the monitor because the function’s single Console. Write. Line() statement is executed (only) when the function is called; after that, execution continues after that call
Another C# Function Example Defining and saving away a “Say. Hello. To” function: static void Say. Hello. To(string name) // one item in the parens { Console. Write. Line("Hello, " + name); } // note that there is a space after the comma in "Hello, " Calling the Say. Hello. To function: string s = "Fred"; Say. Hello. To(s); // or, string s = Console. Read. Line(); // same number of items, just one This prints Hello, Fred on the monitor
A Third C# Function Example Defining and saving away a “Return. Hello” function: static string Return. Hello() // the return type is now string { return "Hello, world!"; // returns a constant string value } Calling the Return. Hello function: string hw = Return. Hello(); // save the function output in hw Now the string variable hw refers to the string "Hello, world!" because the function’s returned value was assigned to hw
C# Function Definitions When you define a function you specify five things: 1. The return type of the function, eg, void or string 2. The name of the function, eg, Say. Hello. To 3. The inputs the function will receive from its caller, if any, specified as parameters (comma-separated typed names in its parentheses), eg, string name 4. How the function works (what it does), specified by the statements inside the function definition { } 5. What the function returns to its caller, if anything, specified by 0 or more return statements inside the function – its output to its caller
Calling a C# Function When you call a function you specify three things: 1. The name of the function, eg, Say. Hello. To 2. The inputs the function will receive, if any, from its caller, specified as arguments (comma-separated items inside its parentheses), eg, "Fred" 3. What to do with a value that the function returns • • Print the value Save the value in a variable by assigning to it Pass the value to another function Anything else … don’t ignore the value
Another C# Function with Return Defining a “Hello. To” function with a returned value: static string Hello. To(string name) { return "Hello, " + name; // return statement } Calling the Hello. To function and saving its output: string output = Hello. To("Fred"); // saved value This sets the variable output equal to "Hello, Fred", essentially “capturing” the function’s output
Function Requirements – 1 How to define a function based on its requirements: 1. If the requirements say the function should ask for/obtain/get/receive input from the user, then it should contain one or more Read. Line() functions • This is a typical way to use Read. Line(): Console. Write("Please enter your name: "); string name = Console. Read. Line(); 2. If the requirements do not say this, then the function should not contain Read. Line() calls; in that case its only inputs come from its parameters!
Function Requirements – 2 How to define a function based on its requirements: 2. If the requirements say the function should print or display output, then it should contain one or more Console. Write. Line() (or Console. Write()) function calls and probably no return statements • If the requirements do not say this, the function should not contain Console. Write. Line() calls; in that case its outputs only go back to its caller via its return statements, if any! • A typical Console. Write. Line() function call is: Console. Write. Line("The answer is" + answer);
More on Calling a C# Function When you call a function with parameters … 1. You specify the arguments to the function (items inside its calling parentheses, separated by commas, one item for each of the parameters) • Essentially those arguments are assigned to the parameters in the same order they are listed 2. A • • • function's arguments (its inputs) can be any of: Literal values like 42, "Fred", or 3. 14159 Variable names like x, var. Name, input_value, … Expressions like x/y, string. Name + "Fred", etc Function or method calls like Console. Read. Line() Any of these must be compatible with their corresponding parameter types
Function Returned Values When you call a function you may receive a value: 1. If the function returns a value then you should use it in some way – print it, use it in an expression, assign it to a variable, etc. 2. If the function does not return a value then its call should appear as a stand-alone statement; in that case the function is being called to produce what is known as a "side effect", like printing something
C# Function Examples – 1 How would you define a function called F that takes no parameters and prints a message? static void F() // function definition line { Console. Write. Line("a message"); // function body } // note: no return statements in this function How would you define a function called F 2 that takes no parameters and returns a string with a message? static string F 2() { return "a message"; } returned. String = F 2(); // function definition line // returns a string // example of how to use F 2
C# Function Examples – 2 How would you define a function called G that takes one string parameter and prints that parameter? static void G(string x) // the parameter name x is arbitrary { Console. Write. Line(x) // it could be param 1, fred, … } // note: use the same name x in both places How would you define a function called G 2 that takes one string parameter and returns that parameter? static string G 2(string x) // again the name x is arbitrary { return x; // the returned value must be a string (x is!) }
C# Function Examples – 3 How would you define a function called H that takes two int parameters and prints their int sum? static void H(int a, int b) // names a and b are arbitrary { Console. Write. Line(a + b); // use a & b like variables } H(2, 3); // a = 2; b = 3; int x = 2, y = 3; H(x, y); // exactly the same: a = x; b = y; How would you define a function called H 2 that takes two int parameters and returns their int sum? static int H 2(int a, int b) // int return type { return a + b; // no need to use parentheses for a + b } // however, return (a + b); also works
- Slides: 17