Writing Functions.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Local and Global Variables. COMP104 Local and Global / Slide 2 Scope The scope of a declaration is the block of code where the identifier is valid for.
CS 117 Spring 2002 Functions Hanly - Chapter 5 Friedman-Koffman - Sections & Chapter 6.
Computer Science 1620 Function Scope & Global Variables.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Covenant College November 27, Laura Broussard, Ph.D. Professor COS 131: Computing for Engineers Chapter 5: Functions.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
CSCI 171 Presentation 6 Functions and Variable Scope.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
Week 5 Part 2 Kyle Dewey. Overview Scope Lifetime Testing Exam #1 overview.
Functions Course conducted by: Md.Raihan ul Masood
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Introduction to Programming
Chapter 7: User-Defined Functions II
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Introduction to the C programming language
C Functions Pepper.
Chapter 5 Functions DDC 2133 Programming II.
Deitel- C:How to Program (5ed)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 - Functions Outline 5.1 Introduction
Global & Local Identifiers
User-Defined Functions
#include "std_lib_facilities
Chapter 5 - Functions Outline 5.1 Introduction
Functions Inputs Output
User Defined Functions
Stack Memory 2 (also called Call Stack)
6 Chapter Functions.
Engineering Computation with MATLAB
Scope Rules and Storage Types
Namespaces How Shall I Name Thee?.
Chapter 6: User-Defined Functions I
Introduction to C++ Programming Language
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Unit-1 Introduction to Java
Programming Fundamental
Corresponds with Chapter 5
Scope Rules.
Presentation transcript:

Writing Functions

The Big Idea What if this was how we did powers?

The Big Idea Functions provide Code Reuse Abstraction

Information Function header specifies everything but job of function A contract to do work

Review A function declaration : Header Body : Statements that do work of the function

A Function A function: -Called max -Takes two inputs (ints) -Gives back an int

Using Max Using Max:

Full Program Must declare a function before it is used

Function Name Function name Normal identifier rules Use camelCase no spaces, can't start with number, etc… Use camelCase Clearly identify job Readability > terseness Focus on verbs

Body Body of a function – where magic happens Always a block of code { } Has own scope Variables declared inside only available inside

Parameters Parameters : information function needs type name, type name, type name… OK to have no parameters int getHourOfDay()…

Parameters Parameters are special variables Only exist within this function Initialized with value caller passes in: abs(10)  number = 10

Function type return pass answer back to caller Must return right type of data

Function type return OK (but not great) to have more than one return Every path must return

Void Functions Void : nothing Void function returns nothing Just does some work Returns at }

Call Stacks Each function works in a separate stack frame in memory

Scope For Functions Scope of variable/parameter restricted to function it is declared in:

Scope For Functions Only variable available are: Parameters New variables you declare in function

Global Something declared outside any function has global scope Available anywhere in file (after declaration)

Global Uses Way to share information Class rules BAD : side effects No variables at global scope Constants OK

Rules For Functions Communicate with params/return Info to function : parameters Info from function : return value

Rules For Functions Do NOT use cin/cout

Rules For Functions Do NOT use cin/cout Unless job of function is to print or get input

Comparison Return a grade: Print a grade:

Why Functions? Provide abstraction Provide reuse