9 - ICS103 Programming in C Lecture 9: Functions I 1...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
1 ICS103 Programming in C Lecture 9: Functions I
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Outline Review about Functions Types of Functions void Functions with Arguments Writing Modular programs using functions Functions with Input Argument and a Single Result Re-usability of Functions Logical Functions Functions with Multiple Arguments Argument List Correspondence The Function Data Area Testing Functions Using Drivers Advantages of Using Function Subprograms Procedural Abstraction Reuse of Functions.
Background image of page 2
3 Review about Functions In chapter 3, we introduced functions as program modules that that perform some operations that contribute towards solving the problem that a C program is designed to solve. We learnt how to use functions from the standard C library such as those in <math.h> and <stdio.h>. We also learnt the steps involved in defining our own (user- defined) functions, namely: Declare the function prototype before the main function Define the detail implementation of the function after the main function Call the function from the main function where its operation is required However, we learnt to write only the simplest type of functions – those that take no argument and return nothing. In this Lecture, we shall learn how to write functions that take arguments, those that return a result and those that do both.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Types of Functions We use function arguments to communicate with the function. There are two types of function arguments: Input arguments – ones that are used to pass information from the caller (such as main function) to the function. Output arguments – ones that return results to the caller from the function. [we shall learn about these in the next lecture] Types of Functions No input arguments, no value returned – void functions without arguments [already discussed in chapter 3] Input arguments, no value returned - void functions with arguments. Input arguments, single value returned. Input arguments, multiple value returned [next lecture]
Background image of page 4
5 void Functions with Input Arguments … A function may take one or more arguments as input but returns no result. Such functions should be declared as void, but each argument should be declared in the bracket following the function name An argument is declared in the same way as variables (its type followed by the name of the argument). Example: void print_rboxed(double rnum); If there are more than one argument, they should be separated by comma. Example: void draw_rectangle(int length, int width); The following function example displays its argument value in a rectangular box.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 void Functions with Input Arguments… /* Uses the print_rboxed function to display a double argument */ #include <stdio.h> void print_rboxed(double rnum); //prototype for the function int main(void) { double x; printf("Enter a double value > "); scanf("%lf", &x); print_rboxed(x);
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

9 - ICS103 Programming in C Lecture 9: Functions I 1...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online