The c Programming language Chapter8 Functions s8.3 The Return Statement o Format: return( expression ) O return expression O return Function The return statement is the mechanism for returning a value from the called function to the calling function ☆Spec >Th as function without returned value ts in a function >If void swap(int x, int y is passed back toi int temp g brace)is en temp-=X sary to the y=temp >Be value
The C Programming Language Chapter 8 Functions §8.3 The Return Statement ❖ Format: return( expression ); or return expression ; or return ; ❖ Function: The return statement is the mechanism for returning a value from the called function to the calling function. ❖ Specifications: ➢There can be zero or more return statements in a function; ➢If there is no return statement,then control is passed back to the calling environment when the closing brace } is encountered; ➢The expression will be converted, if necessary, to the return type of the function . ➢Be notice to the functions needn’t returned value. as function without returned value void swap(int x,int y ) { int temp; temp=x; x=y; y=temp; }
The C Programming language Chapter8 Functions Exp: return garbage printstar( void printstar( printf(求**) printf("****"); mair main( Int a: int a a=printstarO a=printstar( printf("%/od", a) printf("%d", a) output: 10 Compiling error
The C Programming Language Chapter 8 Functions printstar() { printf("**********"); } main() { int a; a=printstar(); printf("%d",a); } Exp : return garbage output:10 void printstar() { printf("**********"); } main() { int a; a=printstar(); printf("%d",a); } Compiling error!
The c Programming language Chapter8 Functions Exp: The expression will be converted to the return type of the function as specified in the function definition mainO i float a, b Int c scanf( %of, %of", &a, &b) c=max(a, b) printf("Max is%dn",c) int max(float x, float y) i float z y!xy, return(z)
The C Programming Language Chapter 8 Functions Exp : The expression will be converted to the return type of the function as specified in the function definition . main() { float a,b; int c; scanf("%f,%f",&a,&b); c=max(a,b); printf("Max is %d\n",c); } int max(float x, float y) { float z; z=x>y?x:y; return(z); }
The c Programming language Chapter8 Functions 8.4 Arguments - Call By Value B Parameter(formal argument)and Argument(actual argument parameter: variables named in the parenthesized list in a function definition >argument: the value used in a call of the function Exp: compare two numbers maino i int a, b c=max(a, b:(main )scanf("%d, %od", &a, &b); arguments max(int x, int y) (max万1c= max(a, b) printf("Max is %d", c); i int z z=>y?: y max(int x, int y parameters returni int return(
The C Programming Language Chapter 8 Functions §8.4 Arguments --- Call By Value ❖Parameter(formal argument) and Argument (actual argument) ➢parameter :variables named in the parenthesized list in a function definition; ➢argument : the value used in a call of the function. c=max(a,b); (main ) max(int x, int y)(max ) { int z; z=x>y?x:y; return(z); } Exp:compare two numbers and output the max main() { int a,b,c; scanf("%d,%d",&a,&b); c=max(a,b); printf("Max is %d",c); } max(int x, int y) { int z; z=x>y?x:y; return(z); } arguments parameters
1 The C Programming Language Chapter8 Functions ☆ Specifications: Arguments must have definit value > Type of the parameter must be specified in the function definition >typically, the arguments match in number and type the parameters K >The arguments will be converted, if necessary, to the type of the parameters >The called function is given the values of its arguments in temporary variables rather than the originals. So the called function cannot directly alter a variable in the calling function. Call-By-Value)
The C Programming Language Chapter 8 Functions ❖ Specifications: ➢Arguments must have definit value; ➢Type of the parameter must be specified in the function definition; ➢Typically,the arguments match in number and type the parameters; ➢The arguments will be converted, if necessary, to the type of the parameters; ➢The called function is given the values of its arguments in temporary variables rather than the originals.So the called function cannot directly alter a variable in the calling function.(Call-By-Value)