Functions. printf( %f cubed is %f,x,cube(x)); return 0; } return parameter type double cube(double var) { return (var*var*var);

Similar documents
C Programming for Engineers Functions

Functions in C C Programming and Software Tools

SUHAIL T A

CS240: Programming in C

Lecture 2: C Programming Basic

Functions BCA-105. Few Facts About Functions:

Chapter 7 Functions. Now consider a more advanced example:

Functions in C C Programming and Software Tools. N.C. State Department of Computer Science

Chapter 11 Introduction to Programming in C

Lecture 9 - C Functions

Technical Questions. Q 1) What are the key features in C programming language?

It is necessary to have a single function main in every C program, along with other functions used/defined by the programmer.

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 11 Introduction to Programming in C

Chapter 11 Introduction to Programming in C

Lecture 5: Methods CS2301

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

Chapter 5 C Functions

Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Chapter 11 Introduction to Programming in C

Array Initialization

Chapter 11 Introduction to Programming in C

Question 2. [2 points] True False By default, structures are passed-by-reference.

Problem Solving in C. Stepwise Refinement. ...but can stop refining at a higher level of abstraction. Same basic constructs. as covered in Chapter 6

Chapter 11 Introduction to Programming in C

DECLARAING AND INITIALIZING POINTERS

M.CS201 Programming language

User Defined Functions

Programming for Engineers Functions

BSM540 Basics of C Language

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

BİL200 TUTORIAL-EXERCISES Objective:

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

Chapter 11 Introduction to Programming in C

CS-201 Introduction to Programming with Java

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 2, Part I Introduction to C Programming

Introduction to Computing Lecture 09: Functions (Part II)

CS240: Programming in C

Ch 4. Parameters and Function Overloading

CSC 2400: Computer Systems. Using the Stack for Function Calls

Lab 3. Pointers Programming Lab (Using C) XU Silei

AMCAT Automata Coding Sample Questions And Answers

EC 413 Computer Organization

CSCI 2132 Software Development. Lecture 18: Functions

Introduction to C/C++ Programming

Programmer-Defined Functions

CSE / ENGR 142 Programming I

CS1150 Principles of Computer Science Methods

Section 2.2 Your First Program in Java: Printing a Line of Text

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Unit 7. Functions. Need of User Defined Functions

User Defined Functions 2 Outline

Full file at

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Question 2. [5 points] Given the following symbolic constant definition

(2-2) Functions I H&K Chapter 3. Instructor - Andrew S. O Fallon CptS 121 (January 18, 2019) Washington State University

MPATE-GE 2618: C Programming for Music Technology. Unit 4.1

CS1150 Principles of Computer Science Methods

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

UNIVERSITY OF WINDSOR Fall 2006 QUIZ # 1. Examiner:Ritu Chaturvedi Dated : Oct 3rd, Student Name: Student Number:

15 FUNCTIONS IN C 15.1 INTRODUCTION

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Chapter 6: Functions

PROGRAMMAZIONE I A.A. 2017/2018

Function Call Stack and Activation Records

Chapter 4 Defining Classes I

CS 101, Spring 2014 April 1st Exam 2 Question 1. [3 points] What output is printed by the following code?

CSE 230 Intermediate Programming in C and C++ Functions

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CpSc 1111 Lab 4 Formatting and Flow Control

Computer Science & Engineering 150A Problem Solving Using Computers

Quiz1 Fall 2007 October 2 nd, UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 1 Solution. Examiner:Ritu Chaturvedi Dated :October 2nd, 2007.

COMP26120: Pointers in C (2018/19) Lucas Cordeiro

CS113: Lecture 4. Topics: Functions. Function Activation Records

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

G Programming Languages - Fall 2012

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

Programming for Electrical and Computer Engineers. Pointers and Arrays

Methods. CSE 114, Computer Science 1 Stony Brook University

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Chapter 12: Pointers and Arrays. Chapter 12. Pointers and Arrays. Copyright 2008 W. W. Norton & Company. All rights reserved.

C Program Structures

Programming Exercise 7: Static Methods

LECTURE 06 FUNCTIONS

Some Computer Preliminaries

PROGRAMMAZIONE I A.A. 2017/2018

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

C Programming Lecture V

Your first C and C++ programs

2. Functions I: Passing by Value

Advanced C Programming Topics

4. C++ functions. 1. Library Function 2. User-defined Function

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

Transcription:

Functions chapter 3 of the text int main(void) double x,y,z; x = cube(y/3.0); printf( %f cubed is %f,x,cube(x)); return 0; return parameter type double cube(double var) return (var*var*var); main() is the caller cube() is the callee main() calls invokes cube() 2 times cube() is called by main() 2 times from 142 F -1

Picturing call and return main() call return call return cube() cube() Can simplify and complete the picture: Operating System main() Static Call Graph: gives the call hierarchy, who calls who cube() printf() 142 F -2

void (1) as a return type The function returns NO result void display_salary(double salary) printf( You earn $%.2f,salary); return /*Optional*/ Write display_salary(mysalary); to call the function 142 F-3

void (2) as a parameter The function takes NO parameter int number(void) int user_number; printf("enter a number: "); scanf("%i",&user_number); return user_number; Write number(); to call the function as a parameter and return type void welcome(void) printf("welcome"); Write welcome(); to call the function 142 F-4

Marching order: Control Flow (order of execution of a program) Begin at main() (no matter where main is) Execute each statement in descending order (top to bottom) UNLESS: function call function return if : Some statements can be skipped loops: some statements can be repeated (see later) 142 F -5

Control Flow: Example CODE void prompt (void) printf("next integer:"); int main (void) prompt(); prompt(); return 0; EXECUTION main prompt( ); prompt( ); prompt printf prompt printf 142 F -6

Functions Prototypes To declare a function Put the prototypes after the # commands e.g. double tax(double income, double rate); The names are arbitrary and can even be omitted. But use them to clarify your program. A prototype specifies how the function is called. The compiler checks any call to a function against the prototype and generates an error if there is a mismatch amount = tax(30000.0); /*error*/ need another argument of type double 142 F-7

Note: when writing code, you can just copy and paste the first line of your function definitions for the function prototypes. #include <stdio.h> /* prototypes */ double tax(double income, double rate); /* function definitions */ int main(void) amount = tax(30000.0, 0.12); return 0; double tax(double income, double rate) /*compute and return the tax */ return income*rate; 142 F-8

Parameters The caller may need to pass the function some values on which to operate input parameters of the function Inputs are specified as formal parameters in function definition double cube(double var) formal input parameter return (var*var*var); When calling the function, the caller provides actual parameters the value of the actual parameter is substituted for the formal parameter int main(void) x=cube(z/2); parameter passing double cube(double var) return (var*var*var); 142F-9

Control and Data Flow When a function is called: (1) control (=execution) transfers to the function body (2) the function executes (3) control returns to the point of call int main(void) double x,y,z; y=6.0; x=cube(y); 216.0 return 0; double cube(double var) 2 return(var*var*var); Note: functions are called by value. var and y are in different locations of the memory. When calling cube, the value of y is copied into var (more on this later). 3 1 6.0 142F-10

Multiple parameters A function can have multiple parameters. The actual parameters (in the call statement) must match the formal parameters (in the header of the function definition) in number, order and type /* in main */ double length_room, width_room; double surface; surface = area(length_room,width_room); match in type, number, and order but can have different names /* definition of area */ double area(double length, double width) return (length*width); 142F-11

Rules for using functions actual parameters MUST match formal parameters in number in order in type a function can ONLY return ONE value In a function that returns type T, the return expression MUST be of type T A function with return type T can be used anywhere an expression of type T can be used. 142F-12

Why have functions? Reuse of program text code it once and use it many times saves space and improve correctness Centralize changes changes and bug fixes are made in one place Better program organization easier to test, understand, and debug Modularization for team projects each person can work independently BETTER: see the forest, and not just the trees The program is shaped in meaningful units. 142F-13

Why have functions? This is how modern programming is done: API Application Programming Interface A library of functions for a particular purpose e.g. Windows API. 142F-14