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

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

View Full Document Right Arrow Icon
1
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 Introduction Arguments and Parameters Call by Value Real World Application: Computing Resistance The Scope of Variables
Background image of page 2
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
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 functions main() must be present where execution begins program terminates when main ends other functions
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 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 { }
Background image of page 7

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

View Full DocumentRight Arrow Icon
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 ) { }
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
11 Main with returned status main() { return EXIT_SUCCESS; } status may be used by operating system
Background image of page 11

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

View Full DocumentRight Arrow Icon
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 ;
Background image of page 12
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);
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
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 Right Arrow Icon
Ask a homework question - tutors are online