EC201- FUNCTIONS CONTROL STATEMENT DTK, JKE, PTSB (V 10.12)

Slides:



Advertisements
Similar presentations
Spring Semester 2013 Lecture 5
Advertisements

Introduction to C Programming
CS1061 C Programming Lecture 2: A Few Simple Programs A. O’Riordan, 2004.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Lecture 1 The Basics (Review of Familiar Topics).
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
Basic Input/Output and Variables Ethan Cerami New York
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Chapter 6: Functions.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Chapter 5: Data Input and Output Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Reading the data from the input devices and displaying the results on the screen are the two main tasks of any program. To perform these tasks user friendly.
Chapter 6: User-Defined Functions
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
C Programming Lecture 10 Instructor: Wen, Chih-Yu Department of Electrical Engineering National Chung Hsing University.
Structure of a C program Preprocessor directive (header file) Program statement } Preprocessor directive Global variable declaration Comments Local variable.
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.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Functions (1)
CPS120: Introduction to Computer Science Lecture 14 Functions.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
UNIT - 4 FUNCTIONS AND POINTERS. FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
User defined functions
FUNCTIONS A function is a set of instructions that can perform a specific task accordingly. A function is a program that performs a specific task according.
CSCI 171 Presentation 6 Functions and Variable Scope.
Principles of Programming - NI Chapter 6: Function In this chapter, you will learn about Introduction to function User define function Function prototype.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
How to design and code functions Chapter 4 (ctd).
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
1 ICS103 Programming in C Lecture 8: Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Functions The fruitful & parameterized functions.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Chapter 1 slides1 What is C? A high-level language that is extremely useful for engineering computations. A computer language that has endured for almost.
Week 4 – Functions Coding Functions. Purpose of Coding Functions A function is written to perform a well-defined task; rather than having all logic in.
EKT120: Computer Programming
User-Written Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
FUNCTION Functions is a sub-program that contains one or more statements and it performs some task when called.
FUNCTIONS.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
EKT120: Computer Programming
CSCI 161: Introduction to Programming Function
User-Defined Functions
Formatted and Unformatted Input/Output Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Functions I Creating a programming with small logical units of code.
Functions, Part 1 of 3 Topics Using Predefined Functions
Function In this lesson, you will learn about Introduction to Function
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

EC201- FUNCTIONS CONTROL STATEMENT DTK, JKE, PTSB (V 10.12)

What is Function DTK, JKE, PTSB (V 10.12) Block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. Every function has a unique name. This name is used to call function from “main()” function. A function can be called from within another function. It also optionally returns a value to the calling program Performs a specific task. A task is a distinct job that your program must perform as a part of its overall operation, such as adding two or more integer, sorting an array into numerical order, or calculating a cube root etc.

Program example: functions DTK, JKE, PTSB (V 10.12) #include kira(int t_semasa, int t_lahir); main() { int t_semasa,t_lahir,u; printf("Please enter your birth year:"); scanf("%d",&t_lahir); printf("Please enter the current year:"); scanf("%d",&t_semasa); u = kira(t_semasa,t_lahir); printf("\nYour age is: %d years old\n\n",u); return 0; } kira(int t_semasa, int t_lahir) { int umur; umur = t_semasa-t_lahir; return(umur); }

Types of functions DTK, JKE, PTSB (V 10.12) Functions with no arguments and no return values.Functions with arguments and no return values.Functions with arguments and return values.Functions that return multiple values.Functions with no arguments and return values.

Functions with no arguments and no return value DTK, JKE, PTSB (V 10.12) A C function without any arguments means you cannot pass data (values like int, char etc) to the called function. Similarly, function with no return type does not pass back data to the calling function. It is one of the simplest types of function in C. This type of function which does not return any value cannot be used in an expression it can be used only as independent statement.

Program example: Functions with no arguments and no return value DTK, JKE, PTSB (V 10.12) #include cetak_line1(); cetak_line2(); main() { printf("Welcome to function in C"); cetak_line1(); printf("Function easy to learn."); cetak_line1(); printf("Please make sure you really understand about this topic."); cetak_line2(); return 0; } cetak_line1() { int i; printf("\n"); for(i=0;i<30;i++) { printf("-"); } printf("\n"); return 0; } cetak_line2() { printf("\n "); return 0; }

Functions with arguments and no return value DTK, JKE, PTSB (V 10.12) In our previous example what we have noticed that “main()” function has no control over the UDF “printfline()”, it cannot control its output. Whenever “main()” calls “printline()”, it simply prints line every time. So the result remains the same. A C function with arguments can perform much better than previous function type. This type of function can accept data from calling function. In other words, you send data to the called function from calling function but you cannot send result data back to the calling function. Rather, it displays the result on the terminal. But we can control the output of function by providing various values as arguments.

Program example: Functions with arguments and no return value DTK, JKE, PTSB (V 10.12) #include tambah(int x, int y); main() { int x,y; printf("Please enter a value x:"); scanf("%d",&x); printf("Please enter a value y:"); scanf("%d",&y); tambah(x,y); return 0; } tambah(int x, int y) { int result; result = x+y; printf("Sum of %d and %d is %d.\n\n",x,y,result); return 0; }

Functions with arguments and return value DTK, JKE, PTSB (V 10.12) This type of function can send arguments (data) from the calling function to the called function and wait for the result to be returned back from the called function back to the calling function. This type of function is mostly used in programming world because it can do two way communications; it can accept data as arguments as well as can send back data as return value. The data returned by the function can be used later in our program for further calculations.

Program example: Functions with arguments and return value DTK, JKE, PTSB (V 10.12) #include tambah(int x, int y); main() { int x,y,z; printf("Please enter a value x:"); scanf("%d",&x); printf("Please enter a value y:"); scanf("%d",&y); z = tambah(x,y); printf("Result %d.\n\n",z); return 0; } tambah(int x, int y) { int result; result = x+y; return(result); }

Functions with no arguments but returns value DTK, JKE, PTSB (V 10.12) We may need a function which does not take any argument but only returns values to the calling function then this type of function is useful. The best example of this type of function is “getchar()” library function which is declared in the header file “stdio.h”. We can declare a similar library function of own.

Program example: Functions with no arguments but returns value DTK, JKE, PTSB (V 10.12) #include send(); main() { int z; z = send(); printf("\nYou entered : %d.", z); return 0; } send() { int no1; printf("Enter a no : "); scanf("%d",&no1); return(no1); }

Functions that return multiple values DTK, JKE, PTSB (V 10.12) We have used arguments to send values to the called function, in the same way we can also use arguments to send back information to the calling function. The arguments that are used to send back data are called Output Parameters. Is a bit difficult for novice because this type of function uses pointer.

Program example: Functions that return multiple values DTK, JKE, PTSB (V 10.12) #include calc(int x, int y, int *add, int *sub); main() { int a,b,p,q; printf("Please enter a value a:"); scanf("%d",&a); printf("Please enter a value b:"); scanf("%d",&b); calc(a,b,&p,&q); printf("Sum = %d, Sub = %d",p,q); return 0; } calc(int x, int y, int *add, int *sub) { *add = x+y; *sub = x-y; return (0); }