BİL527 – Bilgisayar Programlama I

Slides:



Advertisements
Similar presentations
Chapter 7: User-Defined Functions II
Advertisements

Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
GTECH 731 Lab Session 2 Lab 1 Review, Lab 2 Intro 9/6/10 Lab 1 Review Lab 2 Overview.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Subroutines in Computer Programming Svetlin Nakov Telerik Corporation
BIM313 – Advanced Programming Techniques Object-Oriented Programming 1.
Web Services Week 2 Aims: Getting started with creating simple C# applications within Visual Studio.NET Objectives: –An introduction to the syntax of C#.NET.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
BIM313 – Advanced Programming Techniques Strings and Functions 1.
ILM Proprietary and Confidential -
Java Classes. Consider this simplistic class public class ProjInfo {ProjInfo() {System.out.println("This program computes factorial of number"); System.out.println("passed.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX.
Variable Argument Lists CS 112 (slides from Marc Lihan)
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
BİL527 – Bilgisayar Programlama I Functions 1. Contents Functions Delegates 2.
Classes Methods and Properties. Introduction to Classes and Objects In object-oriented programming terminology, a class is defined as a kind of programmer-defined.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
BIL528 – Bilgisayar Programlama II Methods 1. Contents Methods 2.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
C# Programming Methods.
Classes - Intermediate
Methods.
Lecture 6: Methods MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture, you will learn… What a method is Why we use.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
C# Part 1 Intro to C#. Background Designed to be simple, modern, general- purpose, OO, programming language Strong type checking, array bounds checking,
The need for Programming Languages
Test 2 Review Outline.
Creating Your Own Classes
Chapter 7: User-Defined Functions II
C# — Console Application
Static data members Constructors and Destructors
Examples of Classes & Objects
Arrays 3/4 By Pius Nyaanga.
MIS Professor Sandvig MIS 324 Professor Sandvig
USING ECLIPSE TO CREATE HELLO WORLD
CompSci 230 Software Construction
C# Programming Arrays in C# Declaring Arrays of Different Types Initializing Array Accessing Array Elements Creating User Interfaces Using Windows Standards.
Counted Loops.
Programmazione I a.a. 2017/2018.
CS360 Windows Programming
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Starting Out with Java: From Control Structures through Objects
Lecture 3 Functions Simple functions
Pass by Reference, const, readonly, struct
An Introduction to Java – Part I, language basics
Classes & Objects: Examples
Group Status Project Status.
CS150 Introduction to Computer Science 1
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade
Chapter 6 Methods.
Week 4 Lecture-2 Chapter 6 (Methods).
Object Oriented Programming
Methods and Data Passing
Test Review CIS 199 Exam 2 by.
Class.
Classes, Objects and Methods
Arrays 3/4 June 3, 2019 ICS102: The course.
Chapter 6 Arrays.
ITM 352 Functions.
Corresponds with Chapter 5
Presentation transcript:

BİL527 – Bilgisayar Programlama I Functions

Contents Functions

Functions Some tasks may need to be performed at several points in a program e.g. finding the highest number in an array Solution: Write the code in a function and call it several times In object-oriented programming languages, functions of objects are named as methods

Don’t forget to use the word ‘static’! Function Example class Program { static void DrawBorder() Console.WriteLine(“+--------+”); } static void Main(string[] args) DrawBorder(); Console.WriteLine(“| Hello! |”); +--------+ | Hello! | Don’t forget to use the word ‘static’!

Functions that return a value static int ReadAge() { Console.Write(“Enter your age: ”); int age = int.Parse(Console.ReadLine()); if (age < 1) return 1; else if (age > 120) return 120; else return age; }

Functions that take parameters static double Product(double a, double b) { return a * b; }

Functions that take array as parameter class Program { 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);

Parameter Arrays If your function may take variable amount of parameters, then you can use the parameter arrays Think of Console.WriteLine(format, params) You can use any amount of parameters after the format string Parameter arrays can be declared with the keyword params Parameter array should be the last parameter in the parameter list of the function

params Example class Program { 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);

Call By Value and Call By Reference If you change a value-type parameter in the function, it doesn’t affect the value in the main function because, only the value is transferred However, if you change a reference-type parameter’s elements, then the argument in the main function is also changed because the object itself is transferred You can think arrays as reference-types

Call By Value val doubled = 10 myNumber in main: 5 static void ShowDouble(int val) { val *= 2; Console.WriteLine(“val doubled = {0}”, val); } static void Main(string[] args) { int myNumber = 5; ShowDouble(myNumber); Console.WriteLine(“myNumber in main: ” + myNumber); val doubled = 10 myNumber in main: 5

Call By Reference static void ChangeFirst(int[] arr) { arr[0] = 99; } static void Main(string[] args) int[] myArr = {1, 2, 3, 4, 5}; DisplayArray(myArr); ChangeFirst(myArr); 1 2 3 4 5 99 2 3 4 5

ref and out You can change the value of argument in the Main function via a function using the ref keyword You can declare a parameter as the output of a function with the out keyword out parameters does not have to be initialized before the function call (their values are set by the function)

ref Example val doubled = 10 myNumber in main: 10 static void ShowDouble(ref int val) { val *= 2; Console.WriteLine(“val doubled = {0}”, val); } static void Main(string[] args) { int myNumber = 5; ShowDouble(ref myNumber); Console.WriteLine(“myNumber in main: ” + myNumber); val doubled = 10 myNumber in main: 10

out Example static void FindAvgAndSum(int[] arr, out double avg, out int sum) { sum = 0; foreach (int num in arr) { sum += num; } avg = (double) sum / arr.Lenght; static void Main(string[] args) { int sum; double avg; int[] myArray = {3, 4 , 8, 2, 7}; FindAvgAndSum(myArray, out avg, out sum);

Another Example for out 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;

Variable Scope Variables in a functions are not accessible by other functions they are called local variables The variables declared in the class definition are accessible by all functions in the class they are called global variables they should be static so that static functions can access them Variables declared in a block (for, while, {}, etc.) are only accessible in that block

The Main Function The Main function is the entry point in a C# program It takes a string array parameter, whose elements are called command-line parameters The following declarations of Main are all valid: static void Main() static void Main(string[] args) static int Main() static int Main(string[] args)

Command-Line Parameters class Program { static void Main(string[] args) Console.WriteLine("{0} command line arguments were specified:", args.Length); foreach (string arg in args) Console.WriteLine(arg); }

Specifying Command-Line Arguments in Visual Studio Right-click on the project name in the Solution Explorer window and select Properties Select the Debug pane Write command-line arguments into the box Run the application

Specifying Command-Line Arguments in Command Prompt Open a command prompt Go to the folder of the executable output of the program Write the name of the executable followed by the command-line arguments prog.exe 256 myfile.txt “a longer argument”

struct Functions struct CustomerName { public string firstName, lastName; public string Name() return firstName + " " + lastName; }

Calling struct Function CustomerName myCustomer; myCustomer.firstName = "John"; myCustomer.lastName = "Franklin"; Console.WriteLine(myCustomer.Name());

Overloading Functions A function may have several signatures i.e. same function name but different parameters e.g. Console.WriteLine() Intellisense feature of the Visual Studio helps us to select the correct function

Example 1 static int MaxValue(int[] intArray) { … } static double MaxValue(double[] doubleArray) { static void Main(string[] args) { int[] arr1 = { 1, 8, 3, 6, 2, 5, 9, 3, 0, 2 }; double[] arr2 = { 0.1, 0.8, 0.3, 1.6, 0.2}; int maxVal1 = MaxValue(arr1); double maxVal2 = MaxValue(arr2);