04FunctionsLarge

04FunctionsLarge - should have a return statement where the...

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

View Full Document Right Arrow Icon
1 CMSC 106 Lecture Set #4 Set Started: Friday, September 16, 2011 s Function definition s Gives a name to a group of statements which can then be executed (called) just using that name. s Mathematical functions sin(x) = y ^ ^ ^ | | | | | function's result | | | +-- argument | +-- function name s In C, function result is called return value .
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 Defining a function s Functions must be defined to be used. s Definition gives s type of its return value s function's name (same rules as variable names) s names and types of its parameters s its statements (or body) ftype fname(parameterlist) { body of function } s If type omitted an int type is assumed. s If no return type is desired the term void should be used. s If parameter list is empty the term void should be used. s The body
Background image of page 2
Background image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: should have a return statement where the type matches the return type specified. s The fname must be unique. s A function can NOT be defined inside another one. Simple example function definition: void error_msg(void) { printf("This "); printf("is Bad Input\n"); return; } s function name = "error_msg" s return type = nothing s list of parameters = empty s body has only three statements 3 Calling (executing) a function: s general form: s function-name(any arguments) s as a statement: s printf("Hello,"); s or as an expression in assignment: s ch = scanf(%d%d%d,&a,&b,&c); Examples s parameter passing example s return value example s function calling function example...
View Full Document

Page1 / 3

04FunctionsLarge - should have a return statement where the...

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

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