Functions Chapter 3 of the text Motivation:

Slides:



Advertisements
Similar presentations
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Functions Quick Review What is a Function? A module of code that performs a specific job. Examples: Function that determines the maximum of two numbers.
Structure of a C program
CS 201 Functions Debzani Deb.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
CS1061 C Programming Lecture 10: Macros, Casting and Intro. to Standard Library A. O’Riordan, 2004.
1 ICS103 Programming in C Lecture 2: Introduction to C (1)
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
142 F -1 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; } double cube(double.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
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.
CECS 121 Test 1. Functions allow you to group program statements under one name C and C++ are case-sensitive so main(), MAIN(), and Main() are all different.
CC112 Structured Programming Lecture 2 1 Arab Academy for Science and Technology and Maritime Transport College of Engineering and Technology Computer.
CECS 130 EXAM 1. To declare a constant (read only) value: const int x = 20; const float PI = 3.14; Can we do this? const int x;
Khalid Rasheed Shaikh Computer Programming Theory 1.
MODULAR ORGANIZATION Prepared by MMD, Edited by MSY1.
1 Unstructured Programming The Need for Procedures/Functions Procedural Programming Function Declaration/Prototypes/Invocation Example Functions Function.
G3-1 University of Washington Computer Programming I Structuring Program Files © 2000 UW CSE.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Lecture2.
CSCE 206 Structured Programming in C
‘C’ Programming Structures and Commands
Functions Course conducted by: Md.Raihan ul Masood
Chapter 6: User-Defined Functions I
INC 161 , CPE 100 Computer Programming
presented BY : DURGESH KKHANDEKAR 1st semester
C Functions Pepper.
CS1010 Discussion Group 11 Week 4 – Overview of C programming.
Functions, Part 2 of 2 Topics Functions That Return a Value
What's a Computer? Monitor Disk Main mouse Memory Keyboard Network
ICS103 Programming in C Lecture 3: Introduction to C (2)
Getting Started with C.
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Functions in C Mrs. Chitra M. Gaikwad.
Lecture2.
Chapter 5 - Functions Outline 5.1 Introduction
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 5 - Functions Outline 5.1 Introduction
Functions Inputs Output
Compiled and ready to run Memory Stack /*
Functions Declarations CSCI 230
Preprocessor C program → Modified C program → Object Code
Local Variables variables which are declared within a
Chapter 6 - Functions Outline 5.1 Introduction
Functions October 23, 2017.
Lec8.
Functions, Part 2 of 3 Topics Functions That Return a Value
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
In C Programming Language
Introduction to Problem Solving and Programming
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Functions I Creating a programming with small logical units of code.
Assignment Operators Topics Increment and Decrement Operators
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Functions that return a value
Presentation transcript:

Functions Chapter 3 of the text Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To break a program into smaller pieces easier to write reusable for other problems What is a function? It is a named piece of code (e.g. printf) How is it used? It is executed by giving its name It may take some input parameters on which it computes (e.g. printf("7 squared is %i",7*7);) it may compute and return a value (e.g. sqrt(windspeed);) 142 E -1

Examples already seen int main(void) { … Function definition of return 0; } 1 Function definition of the function main 2 printf(“Temperature is %.2f”,celsius); a call to the function printf 3 scanf(“%i”,&age); a call to the function scanf Prewritten functions like scanf and printf are packaged in libraries. #include<stdio.h> tells the compiler to use the standard input/output library (where printf and scanf are) You can use as many libraries as you want (check appendix B for a list of the libraries available with any C compiler) 142 E -2

Useful libraries stdio.h printf, scanf, ... stdlib.h contains many common functions, e.g. random number generators… also contains #define constants e.g. EXIT_SUCCESS math.h x see the list on page AP13 y is pow(x,y) is sqrt(x) ... You can ALSO define your own functions within a program. 142 E -3

Your own functions To write your own function: _ give it a name _ write the code it is to execute Example: write a function to compute the cube of a number. function header function name function parameter function definition double cube(double var) { /*compute and return the cube of var*/ return (var*var*var); } function type (here double) the function MUST return a double Recall: C is strongly typed functions have a type 142 E -4

Calling a function A function can be called from within main or any other function (note that main is just a particular function) int main(void) { double x,y,z; ... x=cube(y/2.0) z=cube(cube(x)); printf(“%f cubed is %f”,y,cube(y)); … return EXIT_SUCCESS; } a function can be anywhere an expression (of the same type) can be. 142 E-5

Declaring your own functions In C, identifiers (=names of things) MUST be declared BEFORE they are used. e.g. need to write int i, before using the variable i For functions, we need to provide a prototype _ after the preprocessor commands (# lines) _ before the first function (the first piece of code between braces {} ), which is generally main just a rule of style (but follow it!) 142E-6

An Example prototype for cube #include <stdio.h> double cube(double value); int main(void) { double x,y; … y=cube(x); return 0; } double cube(double var) return (var*var*var); call to cube definition of cube 142E-7