INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Chapter 7: User-Defined Functions II
Fungsi Risanuri Hidayat, Ir., M.Sc.. Functions C usually consist of two things: instance variables and functions. All C programs consist of one or more.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Computer Science 210 Computer Organization Introduction to C.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 6: User-Defined Functions
Functions Kernighan/Ritchie: Kelley/Pohl: Chapter 4 Chapter 5.
CMSC 1041 Functions II Functions that return a value.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
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 05 Functions II, Storage Class, Scope, rand() METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Created by Harry H. Cheng,  2009 McGraw-Hill, Inc. All rights reserved. C for Engineers and Scientists: An Interpretive Approach Chapter 6: Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
UMBC CMSC 104 – Section 01, Fall 2016
CSCE 206 Structured Programming in C
Functions Course conducted by: Md.Raihan ul Masood
User-Written Functions
Chapter 6 - Functions modular programming general function format
Computer Science 210 Computer Organization
Chapter 7: User-Defined Functions II
Lecture 7: Repeating a Known Number of Times
INC 161 , CPE 100 Computer Programming
FUNCTIONS In C++.
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions and Structured Programming
ICS103 Programming in C Lecture 3: Introduction to C (2)
Quiz 2.
Module 4 Functions – function definition and function prototype.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
CS1061 C Prgramming Lecture 11: Functions
CSCI 161: Introduction to Programming Function
Programmazione I a.a. 2017/2018.
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Computer Science 210 Computer Organization
Chapter 5 - Functions Outline 5.1 Introduction
Functions Declarations CSCI 230
Instructor: Ioannis A. Vetsikas
INC 161 , CPE 100 Computer Programming
Chapter 6 - Functions Outline 5.1 Introduction
Functions and Modular Programming
Functions, Part 2 of 3 Topics Functions That Return a Value
INC 161 , CPE 100 Computer Programming
A function with one argument
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
A First Book of ANSI C Fourth Edition
Chapter 9: Value-Returning Functions
In C Programming Language
CS1100 Computational Engineering
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
Functions in C Math Library Functions Functions Function Definitions
Presentation transcript:

INC 161 , CPE 100 Computer Programming Lecture 8 Function

Introduction A C program is generally formed by a set of functions. These functions subsequently consist of many programming statements. By using functions, a large task can be broken down into smaller ones. Functions are important because of their reusability. That is, users can develop an application program based upon what others have done. They do not have to start from scratch.

Function is a set of collective commands. Walk to a minimart Walk in Search for a bottle of milk Take out a wallet Take out money Give money to the cashier Walk out of the store Buy milk Function

How to use function BuyMilk() { : } Function Definition Main() Function call

In/Out Data of a Function Input (arguments) Data Output (return) Note: Functions can have several arguments but only one return.

Function Definitions A function can be defined in the form of Example: return_type function_name(argument declaration) { statements } Example: int addition(int a, int b) { int s; s = a + b; return s; } main() { int sum; sum = addition(3, 4); printf(“sum = %d\n”, sum);

The function definition has to appear before the real usage. Otherwise, the compiler will not know the meaning of the function. (Try swap the function definition to come after main() and you will get an error.)

Return can be different types from the header definition The return type can be any valid type specifier. The return statement can be used to return a value from the called function to the calling function as in return expression; If necessary, the expression will be converted to the return type of the function. However, if the expression cannot be converted to the return type of the function according to the built-in data type conversion rules implicitly, it is a syntax error. Example: int func(void) { double d; d = 4.6; return d; // OK: C type conversion, return 4 }

Return is necessary if the definition has it If the return type is not void, a return statement is necessary at the end of the function. Otherwise, the default zero will be used as the return value. A calling function can freely ignore the return value. main() is also a function. Example: int func(int i){ return i+1; // the same as ‘return (i+1);’ } int main() { int j; j = func(4); func(5); // ignore the return value return 0;

Return is not necessary if the return type is void If the return type is void, the return statement is optional. However, no expression should follow return; otherwise, it is a syntax error. Example: void func(int i) { if(i == 3) printf("i is equal to 3 \n"); return i; // ERROR: return int } else if(i > 3) printf("i is not equal to 3 \n"); return; // OK i = -1; // return not necessary since return type is void int main(){ func(2); return 0; Note: void can be omitted

Input can be different types from the header definition The data type of an actual argument of the calling function can be different from that of the formal argument of the called function as long as they are compatible. The value of an actual argument will be converted to the data type of its formal definition according to the built-in data conversion rules implicitly at the function interface stage. Example: int func1(int i) { // argument type is int return 2*i; } int main(){ func1(5); // OK func1(5.1); // OK, 5.1 converted to 5 return 0;

main() is also a function The C complier looks for function name “main” to use it as entry point. Easy format With error code format main () { } int main () { return 0; } Error code 0 = finish program normally

Example Program 1: Output: to calculate the external force. /* File: accelfun.c */ #include <stdio.h> #define M_G 9.81 double force(double t) { double p; p = 4*(t-3)+20; return p; } int main() { double a, mu, m, p, t; mu = 0.2; m = 5.0; t = 2.0; p = force(t); a = (p-mu*m*M_G)/m; printf("Acceleration a = %f (m/s^2)\n", a); return 0; Example Program 1: Use a function to calculate the external force. Output: Acceleration a = 1.238000 (m/s^2)

Example Program 2: Use functions with multiple arguments. /* File: accelfunm.c */ #include <stdio.h> #define M_G 9.81 double force(double t) { double p; p = 4*(t-3)+20; return p; } double accel(double t, double mu, double m) { double a, p; p = force(t); a = (p-mu*m*M_G)/m; return a; int main() { double a, mu, m, t; mu = 0.2; m = 5; t = 2; a = accel(t, mu, m); printf("Acceleration a = %f\n (m/s^2)", a); return 0; Example Program 2: Use functions with multiple arguments.

Function Prototypes A function prototype is a declaration of a function that declares the return type and types of its parameters. For example, double force(double t); double accel(double t, double mu, double m); A function prototype contains no function definition. A function prototype can be declared at any lexical level, before and after its function definition. If the function is called before it is defined, int is assumed to be the return type of the function. Therefore, a function prototype is required if the return type of the function is not int and the function is called before its definition is processed, or if the function definition is located in a different file or in a library.

Example Program 3: Use function prototypes. /* File: accelprot.c */ #include <stdio.h> #define M_G 9.81 double force(double t); double accel(double t, double mu, double m); int main() { double a, mu, m, t; mu = 0.2; m = 5.0; t = 2.0; a = accel(t, mu, m); printf("Acceleration a = %f (m/s^2)\n", a); return 0; } double force(double t) { double p; p = 4*(t-3)+20; return p; double accel(double t, double mu, double m) { double a, p; p = force(t); a = (p-mu*m*M_G)/m; return a; Example Program 3: Use function prototypes.

Standard C Header Files and Libraries Header files typically contain function prototypes for performing operations that are related to one another. They may also include macros and type definitions required by some of the functions. For example, header file math.h contains the function prototypes extern double sin(double x); extern double exp(double x); for mathematical functions sin(x) and ex. The type qualifier extern for functions defined in a library or other module. printf() , scanf() are functions defined in stdio.h library. This is why we have to add the line #include <stdio.h>

Some Standard C Header Files Description limits.h Define several macros that expand to various limits and parameters of the standard integer types. float.h Define several macros that expand to various limits and parameters of the standard floating-point types. math.h Declare two types and several functions and define several macros for general mathematical operations. stdbool.h Define macros for boolean operations stdio.h Declare types, macros, and functions for standard input and output. stdlib.h Declare several types, macros, and functions for general utility, such as conversion of numbers to text and text to numbers, memory allocation, and generation of random numbers. time.h Define macros and declare sever types and functions for manipulating time and date.

Recursive Functions Functions may be used recursively. This means that a function can call itself directly. When a function calls itself recursively, each function call will have a new set of local variables. Recursive functions usually contain conditional statements, such as if-else, to allow exit of the function and return control to the calling function. A function may also call itself indirectly.

Example: Calculating a factorial 5! using a function with a for-loop. The factorial n! is defined as n*(n-1)! Output: 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 /* File: factorloop.c */ #include <stdio.h> unsigned int factorial(int n); int main() { int i; for(i=0; i<=5; i++) printf("%d! = %d\n", i, factorial(i)); return 0; } unsigned int factorial(int n) { unsigned int i, f; for(i=1, f=1; i<=n; i++) { f *= i; return f;

Example: Calculating a factorial 5! using a recursive function. /* File: factorrecursive.c */ #include <stdio.h> unsigned int factorial(int n); int main() { int i; for(i=0; i<=5; i++) printf("%d! = %d\n", i, factorial(i)); return 0; } unsigned int factorial(int n) { if(n <= 1) { return 1; else { return n*factorial(n-1); Output: 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120

int main( ){ int f = 2; } int main(){ int f = factorial(2); } int factorial(int n){ // n=2 if(n<=1) return 1; else return 2*factorial(1); } int factorial(int n){ // n=2 if(n<=1) return 1; else return n*factorial(n-1); } int factorial(int n){ // n=2 if(n<=1) return 1; else return 2*1; } int factorial(int n){ // n=1 if(n<=1) return 1; else return n*factorial(n-1); }