CHAPTER_5

# CHAPTER_5 - 1 Introduction Arguments and Parameters Call by...

This preview shows pages 1–14. Sign up to view the full content.

1

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

View Full Document
2 Introduction Arguments and Parameters Call by Value Real World Application: Computing Resistance The Scope of Variables
What is a Function? A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required. 3

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

View Full Document
4 functions main() must be present where execution begins program terminates when main ends other functions
Parts of Function There are two main parts of the function. The function header and the function body. int sum(int x, int y) { int ans = 0; //holds the answer that will be returned ans = x + y; //calculate the sum return ans //return the answer } 5

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

View Full Document
Function Header In the first line of the above code int sum(int x, int y) It has three main parts: The name of the function i.e. sum The parameters of the function enclosed in paranthesis Return value type i.e. int 6
7 FUNCTION Header int gamma(int x,float y,double z) float alpha(char u) char abc(char w) void beta(float t,float u) float prin(void) spd(float x,char y) int spd(float x,char y) Body { }

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

View Full Document
8 char grade ( int exam1, int exam2, float exam1_weight ) { float average; average = exam1_weight * exam1 + (1.0 - exam1_weight ) * exam2; if ( average > 7.0 ) return ‘P’; else return ‘F’; } Function’s name data type returned body left brace right brace parameters header The function echo_line has no parameters and return no value void echo_line ( void ) { }
9 return statement main() calls (invokes) a function f function f runs and then returns to main statement: return; return x; return (exprn); return (12 + 6 +fun1(m)); return x1,x2; returns x2 ( comma operator p.100 ) a function may contain several return statements

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

View Full Document
10 Declaration and definition Declaration char grade(int a,int b, int c); Definition char grade(int a,int b,int c) { } Each function’s definition must be in its own file
11 Main with returned status main() { return EXIT_SUCCESS; } status may be used by operating system

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

View Full Document
12 #include <stdio.h> main ( ) { char grade ( int exam1, int exam2, float exam1_weight ); /* function- */ int ans, mid_term, final; /*declaration */ float weight; char letter_grade; letter_grade = grade ( mid_term, final, weight ); /* invoke function */ } char grade ( x1, x2, x3 ) { int x1,x2; float x3 ... return; } Note the ; Note: NO ;
13 In a declaration, the names that follow the data types of the parameters are optional and are ignored by the compiler. The names need not be the same as the names of the parameters. In a definition, the parameters must always be named. char grade (int, int, float);

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/26/2011 for the course ECE 3331 taught by Professor Staff during the Fall '08 term at University of Houston.

### Page1 / 72

CHAPTER_5 - 1 Introduction Arguments and Parameters Call by...

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

View Full Document
Ask a homework question - tutors are online