Function Example. Function Definition. C Programming. Syntax. A small program(subroutine) that performs a particular task. Modular programming design

Similar documents
BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

CSE123. Program Design and Modular Programming Functions 1-1

C Functions. 5.2 Program Modules in C

Functions. Systems Programming Concepts

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

Chapter 3 - Functions

C++, How to Program. Spring 2016 CISC1600 Yanjun Li 1

Introduction to Programming

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Programming Fundamentals for Engineers Functions. Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. Modular programming.

C Functions Pearson Education, Inc. All rights reserved.

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

C++ PROGRAMMING SKILLS Part 3 User-Defined Functions

Methods: A Deeper Look

Functions and Recursion

Fundamentals of Programming & Procedural Programming

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

C Programs: Simple Statements and Expressions

INTRODUCTION TO C++ FUNCTIONS. Dept. of Electronic Engineering, NCHU. Original slides are from

Introduction to C Language

Functions. Functions are everywhere in C. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

C++ Programming Lecture 11 Functions Part I

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

CT 229 Java Syntax Continued

Methods (Deitel chapter 6)

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 3 - Functions

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Methods (Deitel chapter 6)

Functions. Prof. Indranil Sen Gupta. Dept. of Computer Science & Engg. Indian Institute t of Technology Kharagpur. Introduction

ECET 264 C Programming Language with Applications

1001ICT Introduction To Programming Lecture Notes

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

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

Fall Semester (081) Dr. El-Sayed El-Alfy Computer Science Department King Fahd University of Petroleum and Minerals

Downloaded from Chapter 2. Functions

Function. specific, well-defined task. whenever it is called or invoked. A function to add two numbers A function to find the largest of n numbers

Assoc. Prof. Dr. Tansu FİLİK

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

Basic types and definitions. Chapter 3 of Thompson

Using Free Functions

Computer Science & Engineering 150A Problem Solving Using Computers

Computer Programming 5th Week loops (do-while, for), Arrays, array operations, C libraries

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

Object-Based Programming. Programming with Objects

Functions. Autumn Semester 2009 Programming and Data Structure 1. Courtsey: University of Pittsburgh-CSD-Khalifa

Unit 7. Functions. Need of User Defined Functions

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

The Math Class. Using various math class methods. Formatting the values.

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

C: How to Program. Week /Apr/16

Chapter 5 C Functions

C Functions. Object created and destroyed within its block auto: default for local variables

User Defined Functions

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Lecture 04 FUNCTIONS AND ARRAYS

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

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

Built-in Types of Data

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

LAB 7 FUNCTION PART 2

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

(created by professor Marina Tanasyuk) FUNCTIONS

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

Functions. A function is a subprogram that performs a specific task. Functions you know: cout << Hi ; cin >> number;

Programming Language. Functions. Eng. Anis Nazer First Semester

Chapter 6 - Notes User-Defined Functions I

Chapter 4: Basic C Operators

Object Oriented Programming Using C++ Mathematics & Computing IET, Katunayake

DM536 / DM550 Part 1 Introduction to Programming. Peter Schneider-Kamp.

Methods CSC 121 Fall 2014 Howard Rosenthal

Standard Library Functions Outline

Mentor Graphics Predefined Packages

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

What is a Function? What are functions good for?


Methods CSC 121 Fall 2016 Howard Rosenthal

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

MATHEMATICAL / NUMERICAL FUNCTIONS

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Chapter 4 Functions By C.K. Liang

Methods CSC 121 Spring 2017 Howard Rosenthal

Function I/O. Last Updated 10/30/18

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

Fundamentals of Programming Session 13

Computing and Statistical Data Analysis Lecture 3

Functions. Lecture 6 COP 3014 Spring February 11, 2018

cs3157: another C lecture (mon-21-feb-2005) C pre-processor (3).

Visual Studio. Visual Studio is an extremely complex interactive development environment capable of handling many languages and tools.

The Math Class (Outsource: Math Class Supplement) Random Numbers. Lab 06 Math Class

2. Numbers In, Numbers Out

Programming in C Quick Start! Biostatistics 615 Lecture 4

Script started on Thu 25 Aug :00:40 PM CDT

Intrinsic Functions Outline

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

Transcription:

What is a Function? C Programming Lecture 8-1 : Function (Basic) A small program(subroutine) that performs a particular task Input : parameter / argument Perform what? : function body Output t : return value Modular programming design Large and complex task can be divided into smaller and simple task which is more easily solved(implemented). Also called structured t design Top-down design Divide-and-Conquer Function Example Syntax Function Definition iti int f(int val); // function prototype declaration // int f(int); is also OK! int x,y; scanf( %d,&x); y = f(x); // function call. x is argument. printf( y=%d, y); int f(int val) // function definition. val is parameter. int k; k = 2*val 3 ; return k; return_type type function_name name (data_type type variable_name, ) local declarations; // local variables function statements; Example int factorial (int n) int factorial (int n) int i,product=1; for (i=2; i<=n; ++i) product *= i; return product; return product;

void type Variables Example) void print_info(void) printf("navier-stokes Equations Solver "); printf("v3.45\n"); printf("last Modified: "); printf("12/04/95 - viscous coefficient added\n"); return type is void No parameter Global variable Declared outside function block Accessible everywhere Global variable is destroyed only when a program is terminated. Local variable (automatic variable?) Declared inside function body Accessible only in the function Local variable is created when a function is called and is destroyed when a function returns. Static variable (declared in a function) (Usually) accessible in the function Static variable persists until the program is terminated // example printf("local x in main's outer block is %d\n,x); void uselocalscope( void ); // function prototype void usestaticlocalscope( void ); // function prototype void useglobalscope( void ); // function prototype int x = 1; // global variable int x = 5; // local variable to main printf("local x in main's outer block is %d\n",x); uselocalscope(); usestaticlocalscope(); useglobalscope(); uselocalscope(); usestaticlocalscope(); useglobalscope(); printf("\nlocal x in main s outer block is %d\n,x); // start new block // end main int x = 7; printf("local x in main's inner block is %d\n",x); // end new block

// uselocalscope void uselocalscope( void ) int x = 25; // initialized iti each time this function is called. printf("local x is %d on entering uselocalscope()\n,x); ++x; printf("local x is %d on exiting uselocalscope()\n,x); // end function uselocalscope void usestaticlocalscope( void ) // x is initialized only first time usestaticlocalscope is called. // It s value is kept till the next call. static int x = 50; printf("local static x is %d on entering usestaticlocalscope()\n,x); ++x; // increment x printf("local static x is %d on exiting usestaticlocalscope()\n,x); // end function usestaticlocal // useglobal modifies global variable x during each call void useglobalscope( void ) // modifies global variable x during each call. printf("global x is %d on entering useglobalscope()\n,x); x *= 10; // multiply 10 to x printf("global x is %d on exiting useglobalscope()\n,x); // end function useglobalscope() local x in main's outer block is 5 local x in main's inner block is 7 local x in main's outer block is 5 local x is 25 on entering uselocalscope() local x is 26 on exiting uselocalscope() local static x is 50 on entering usestaticlocalscope() local static x is 51 on exiting usestaticlocalscope() global x is 1 on entering useglobalscope() global x is 10 on exiting useglobalscope() output local x is 25 on entering uselocalscope() local x is 26 on exiting uselocalscope() local static x is 51 on entering usestaticlocalscope() local static x is 52 on exiting usestaticlocalscope() global x is 10 on entering useglobalscope() global x is 100 on exiting useglobalscope() local x in main s outer block is 5

Variables You must understand the difference between Global vs Local variables Static vs Global variables Static vs Local(Automatic) variables Considerations (1) The number of arguments in the function call must match the number of arguments in the function definition. The type of the arguments in the function call must match the type of the arguments in the function definition. The type of actual return value must match the type of return type in function prototype. Before calling a function, either function definition or function prototype declaration must be done. Considerations (2) The actual arguments in the function call are matched up in- order with the dummy arguments in the function definition. The actual arguments are passed by-value to the function. The dummy arguments in the function are initialized with the present values of the actual arguments. Any changes made to the dummy argument in the function will NOT affect the actual argument in the main program. Why use functions? Many, many reasons Don t have to repeat the same block of code many times. Make that code block a function and call it when needed. Reuse : useful functions can be used in a number of programs. top-down technique : Make an outline and hierarchy of the steps needed to solve your problem and create a function for each step. Easy to debug : Get one function working well then move on to the others. Easy to modify and expand : Just add more functions to extend program capability Readibilty : Make program self-documenting and readable.

Math Library Functions (example) #include <math.h> // you must include <math.h> // to use math functions double c, a, b; scanf( %lf %lf,&a,&b); c=sqrt(pow(a,2)+pow(b,2)); printf( a^2+b^2=%lf\n,c); Method Description Example ceil( x ) rounds x to the smallest integer ceil( 9.2 ) is 10.00 not less than x ceil( -9.8 ) is -9.0 cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0 exp( x ) exponential function ex exp( 1.0 ) is 2.71828 exp( 2.0 ) is 7.38906 fabs( x ) absolute value of x fabs( 5.1 ) is 5.1 fabs( 0.0 ) is 0.0 fabs( -8.76 ) is 8.76 floor( x ) rounds x to the largest integer not greater than x floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 fmod( x, y ) remainder of x/y as a fmod( 13.657, 2.333 ) is 1.992 floating-point number log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0 log10( x ) logarithm of x (base 10) log10( 10.00 ) is 1.0 log10( 100.0 ) is 2.0 pow( x, y ) x raised to power y (x^y) pow( 2, 7 ) is 128 pow( 9,.5 ) is 3 sin( x ) trigonometric sine of x sin( 0.0 ) is 0 sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 tan( x ) trigonometric tangent of x tan( 0.0 ) is 0 Math library functions.