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.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Introduction to C Programming
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
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.
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Metode di Java Risanuri Hidayat, Ir., M.Sc.. Pendahuluan Classes usually consist of two things: instance variables and methods. The topic of methods is.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 8: Functions, File IO.
CMSC 1041 Functions II Functions that return a value.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
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.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
A First Book of ANSI C Fourth Edition Chapter 6 Modularity Using Functions: Part I.
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.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
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.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
UMBC CMSC 104 – Section 01, Fall 2016
Functions Course conducted by: Md.Raihan ul Masood
C Characters and Strings
INC 161 , CPE 100 Computer Programming
FUNCTIONS In C++.
Functions, Part 2 of 2 Topics Functions That Return a Value
Functions and Structured Programming
Quiz 11/15/16 – C functions, arrays and strings
Programming Fundamentals Lecture #7 Functions
Deitel- C:How to Program (5ed)
Programmazione I a.a. 2017/2018.
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Data Type.
Chapter 5 - Functions Outline 5.1 Introduction
Data Type.
Chapter 6 - Functions Outline 5.1 Introduction
CS 161 Introduction to Programming
بنام خدا زبان برنامه نویسی C (21814( Lecture 4 Chapter 5
Functions, Part 2 of 3 Topics Functions That Return a Value
A First Book of ANSI C Fourth Edition
The Standard Library Chapter 21
Functions in C and C++ CS-2303 System Programming Concepts Hugh C. Lauer (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
In C Programming Language
CSC215 Lecture Introduction.
Data Type.
Programming Languages and Paradigms
Characters and Strings Functions
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
C Characters and Strings
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:

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 functions. Functions are the building blocks of a program. All functions are at the same level — there is no nesting. One (and only one) function must be called main.

general form This is the general form of a function: type name(parameter-list) { // body of function return value; } type specifies the type of data returned by the function. If the function does not return a value, its return type must be void. The name of the function is specified by name. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the function when it is called. value is the value returned.

return All functions can return a value, including main. Functions can return arithmetic values ( int, float etc.), structures, unions, pointers or void. If the return type is specified as being void, then no value is returned by the function. Functions cannot return a function or an array.

Parameters All functions (including main) can accept parameters. void volume(double x, double y, double z) { printf(“%f \n”, x*y*z); } The parameter list must be specified, and their types must be declared. The parameters to the function (the expressions given in the function call) are passed by value only. If,the parameter list contains the single word void, then the function does not take any parameters.

arguments It is common to call the variables specified in the function definition parameters and the expressions given in a function call arguments. For example, in the following call of volume, the expressions 10, 20, and 15 are the arguments to the function. The values of the two expressions will be copied into the parameters x, y, and z. Sometimes the terms formal argument and actual argument are used instead; the formal argument being the variable given in the function definition, the actual argument being the expression given in the function call. volume(10, 20, 15);

Example // Metode example (Metode.c). #include void volume(double x, double y, double z) { printf(" %lf \n",x*y*z); } main() { double vol; double width = 10; double height = 20; double depth = 15; printf("Volume is "); volume(width, height, depth); }

Example 2: Returning value // (metode01.c) #include double volume(double x, double y, double z) { double v; v =x*y*z; return v; } main() { double vol; double width = 10; double height = 20; double depth = 15; vol = volume(width, height, depth); printf("Volume is "); printf(" %lf \n",vol); }

Function overloading C tidak mendukung Function overloading

Type Conversion When a function is called, C looks for a match between the arguments used to call the function and the function's parameters. However, this match may not always be exact. In some cases C can automatically convert the type of arguments to the type of parameters.

Type Conversion // Demonstrate function overloading (overload01.c). #include double test(double a) { printf("double a: %lf \n", a); return a*a; } main() { int x=10; double result; // call all versions of test() result = test(x); printf("Result of test(123.2): %lf \n", result); } //main

recursion C supports recursion. Recursion is the process of defining something in terms of itself. recursion is the attribute that allows a function to call itself. A function that calls itself is said to be recursive

recursion // Demo recursion (recursion01.c) #include int fact(int n) { int result; if(n==1) return 1; result = fact(n-1) * n; return result; } main() { printf("Factorial of 3 is %d \n", fact(3)); printf("Factorial of 4 is %d \n", fact(4)); printf("Factorial of 5 is %d \n", fact(5)); } //main

Standard Header Standard Header Files Prototypes of the library functions are given in several standard header files. For example, stdio.h contains prototypes for printf, scanf, putchar and getchar. Other standard header files are: –assert.h assertions –ctype.h character class tests –float.h system limits for floating point types –limits.h system limits for integral types –math.h mathematical functions –setjmp.h non-local jumps –signal.h signals and error handling

Standard Header –stdarg.h variable length parameter lists –stdlib.h utility functions; number conversions, memory allocation, exit and system, Quick Sort –string.h string functions –time.h date and time functions To include these standard header files in your code, use the preprocessor directive #include and place angle brackets around the name of the file, e.g. #include