07 Functions I
07 Functions I
Output:
1 4 9 16 25 36 49 64 81 100
square Function: Calling Function
• square is invoked or called in line 8:
• printf("%d ", square(x)); // function call
• Receives a copy of the argument x’s value in the parameter number
(line 12).
• Calculates number * number and passes the result back to line 8
in main where square was invoked.
• Line 8 passes square’s result to function printf, which displays
the result on the screen
square Function: Function Definition
• square’s definition (lines 12–14) shows that it expects an int parameter
number
• int preceding the function name (line 12) indicates square returns an
integer result
• return statement in square passes the result of number * number
back to the calling function.
• Choosing meaningful function names and meaningful parameter names
makes programs more readable and helps avoid excessive comments
• A function requiring a large number of parameters may be performing too
many tasks
• return type, name and parameter list should fit on one line if possible
square Function: Local Variables
• All variables defined in function definitions are local variables
• They can be accessed only in the function in which they’re defined
• Most functions have parameters that enable communicating
between functions via arguments in function calls
• Parameters are also local variables of that function.
square Function: Function Prototype
• Line 3 is a function prototype
• int square(int number); // function prototype
• int in parentheses informs the compiler that square expects to receive an integer
value from the caller
• int to the left of the function name informs the compiler that square returns an
integer result to the caller.
• Forgetting the semicolon at the end of a function prototype is a syntax error
• The compiler compares square’s call (line 8) to its prototype to ensure that:
• the number of arguments is correct,
• the arguments are of the correct types,
• the argument types are in the correct order, and
• the return type is consistent with the context in which the function is called.
square Function: Function Prototype
• The function prototype, first line of the function definition and function calls
should all agree in the number, type and order of arguments and parameters
• The function prototype and function header must have the same return type
Format of a Function Definition
• The format of a function definition is
• return-value-type function-name(parameter-list) {
statements
}
• function-name is any valid identifier
• return-value-type is the type of result returned to the caller
• void indicates that a function does not return a value
• return-value-type, function-name and parameter-list are the function header
• parameter-list is a comma-separated list of parameters required to call the function
when it’s called
• void for no parameters.
• Each parameter must include its type
• Placing a semicolon after the parameter-list’s right parenthesis in a function definition is
an error
Function Body
• The statements within braces form the function body, which also is a
block
• Local variables can be declared in any block, and blocks can be nested
• Functions cannot be nested
• defining a function inside another function is a syntax error
Returning Control from a Function
• There are three ways to return control from a called function to the
point at which a function was invoked
• If the function does not return a result, control is returned simply
when the function-ending right brace is reached, or by executing the
statement
• return;
• If the function does return a result, the following statement returns
the expression’s value to the caller.
• return expression;
Main’s Return Type
• main’s int return value indicates whether the program executed
correctly
• The C standard indicates that main implicitly returns 0 if you omit
the return statement at the end of main
• You can explicitly return nonzero values from main to indicate that a
problem occurred during your program’s execution
maximum Function
• Consider a custom maximum function that returns the largest of
three integers.
• maximum returns its result to main via the return statement in
line 28
• The printf statement in line 14 then prints the value returned by
maximum
maximum Function
1. // Finding the maximum of three integers.
2. #include <stdio.h>
3.
4. int maximum(int x, int y, int z); // function prototype
5.
6. int main(void) {
7. int number1 = 0; // first integer entered by the user
8. int number2 = 0; // second integer entered by the user
9. int number3 = 0; // third integer entered by the user
10.
11. printf("%s", "Enter three integers: ");
12. scanf("%d%d%d", &number1, &number2, &number3);
13.
14.printf("Maximum is: %d\n", maximum(number1, number2,
number3));
15.}
maximum Function
16.// Function maximum definition
17.int maximum(int x, int y, int z) {
18. int max = x; // assume x is largest
19.
20. if (y > max) { // if y is larger than max,
21. max = y; // assign y to max
22. }
23.
24. if (z > max) { // if z is larger than max,
25. max = z; // assign z to max
26. }
27.
28. return max; // max is largest value
29.}
Function Prototypes
• Compiler uses function prototypes to validate function calls
• Include function prototypes for all functions to take advantage of C’s
type-checking capabilities
• A function prototype (omitting the semicolon) is generally the same
as the functions definition’s first line without the semicolon
• Include parameter names in function prototypes for documentation
purposes
• The compiler ignores these names
Function Prototypes
• Another important feature of function prototypes is argument
coercion
• implicitly converting arguments to the appropriate type
• In general, argument values that do not correspond precisely to the
function prototype’s parameter types are converted to the proper
type before the function is called
• Such conversions can lead to incorrect results
• For example, converting a double to an int may lose data
Function Prototypes
• If there’s no function prototype for a function, the compiler forms
one from the first occurrence of the function—either the function
definition or a call to the function. This typically leads to warnings or
errors, depending on the compiler.
• Always include function prototypes for the functions you define or
use in your program to help prevent compilation errors and warnings.
• A function prototype placed outside any function definition applies to
all calls to the function appearing after the function prototype. A
function prototype placed in a function body applies only to calls in
that function made after that prototype.
Reference
• C How to Program, Ninth Edition by Deitel & Deitel, Pearson, 2022.