Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Procedural programming in Java
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 Programming in C++ Lecture Notes 9 Functions (Returning Values) Andreas Savva.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
CS 201 Functions Debzani Deb.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
Functions in C. Consider #include main() { int i; for(i=1; i
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Procedures and Functions Computing Module 1. What is modular programming? Most programs written for companies will have thousands of lines of code. Most.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
CMSC 1041 Functions II Functions that return a value.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Procedural programming in Java Methods, parameters and return values.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
Definition of function Types of Function Built-in User Defined Catagories of Function No argument No return value Argument but No return value No argument.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
FUNCTIONS - What Is A Function? - Advantages Function Declaration
1 ICS103 Programming in C Lecture 8: Functions I.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Functions. Example u int max(int a, int b); int main(){ int x; x = max(5,8); x = max(x,7); } int max(int a, int b){ return a>b?a:b; }
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions in C Mrs. Chitra M. Gaikwad.
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Method Mark and Lyubo.
Functions I Creating a programming with small logical units of code.
Stack Memory 2 (also called Call Stack)
Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Procedure Activations
Function In this lesson, you will learn about Introduction to Function
Functions Extra Examples.
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Presentation transcript:

Week 4 – Functions Coding Functions

Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in main you should split it into smaller functions and call these functions from main A function normally requires some data to be passed to it (parameters) A function normally returns a result to where it was called from in the program If no standard function exists for the task you need to do then you will have to code your own function

Function Syntax datatype functioname(datatype formal_parameter1, datatype formal_parameter2, …) { function coding } eg. double f1(int val, double num) { double num2; return n2; } a function may have a datatype or may be of type void a function may have 0, 1 or more formal parameters which are used to refer to values passed to a function. Each parameter must be defined as a datatype a function can contain within { } any valid C statements a function may or may not contain a return statement

Return Statement: Purpose A function normally returns a result Eg. the square root function should return the square root of the value passed to it; i.e. passing a value of 9 to the sqrt function should return a value of 3 The result from a function in C is returned using the return statement A function can have 0, 1 or more than one return statement If a function does not have a return statement the datatype of the function must be void; if a function uses a return the function must be defined with the datatype that it returns When a return statement is executed control is returned back to the statement from which the function was called

Calling a Function Execution always starts at the beginning of "main“ A function is called or “invoked” by using its name within a program Actual values or variables must be provided corresponding to the parameters if a function has parameters Example: double f1(int val, double num) { double res = val * num; return res; } main( ) double num2, num1 = 2.2; int num3 =3; num2 = f1(num3, num1); printf(“num2:%d\n”,num2); }

Function Example using Return Statement The getnum function does not require any information to be passed to it so it has no parameters and therefore its parameter list is void. However the function returns a value of datatype int and therefore must be defined as a function of datatype int. int getnum(void) { int num; do { printf("Enter number(>0):"); scanf("%d",&num); } while (num <= 0); return num; }

Program Example using Return int getnum(void) { int num; do { printf("Enter number(>0):"); scanf("%d",&num); } while (num <= 0); return num; } main() { int age, cont; do { age = getnum(); if (age < 19) printf (“Youth\n"); else if (age < 66) printf("Adult\n"); else printf("Senior\n"); printf("Do you want to continue(1(Y)/0(N))?\n"); scanf("%d",&cont); } while (cont == 1); }

More Information about Function main The operating system calls the main function, so the return value and parameters of main are used to communicate with the operating system int main (int argc, char *argv[]) could be used to call your program with arguments and to return an integer return code (or exit code or exit status) By Unix/Linux standards, an exit status of "0" indicates successful program execution, anything other than zero indicates failure Therefore main normally should return 0 to the operating system

Program Example using Return in main int getnum(void) { int num; do { printf("Enter number(>0):"); scanf("%d",&num); } while (num <= 0); return num; } int main( ) { int age, cont; do { age = getnum(); if (age < 19) printf (“Youth\n"); else if (age < 66) printf("Adult\n"); else printf("Senior\n"); printf("Do you want to continue(1(Y)/0(N))?\n"); scanf("%d",&cont); } while (cont == 1); return 0; }