Functions Flashcards
A function that has a return type other than void must
have a return statement
Calling a Function in the Same .c file
In C you may only call a function in a file after it has been declared (you can call it before it has
been defined, but only after it has been declared).
If the type of the data provided as an argument doesn’t match the type of the
parameter
it will perform an implicit type conversion of the argument to the type of the parameter
without telling you (if such a conversion is possible). If such a conversion is not possible, there
will a compiler error.
Order of the typecast in implicit conversion.
bool -> char -> short int -> int -> unsigned int -> long int -> unsigned long int -> long long int -> float -> double -> long double
rule of implicit conversion
Generally, it is ok to pass a numeric argument that
has a smaller bit-width than the type expected, but not vice versa.
passing a long int where an int is expected can be disastrous because the type conversion will be achieved
by removing some bits of the long int to make it fit into the memory space for an int which
potentially modifies the value being passed!
What should be known when Calling Functions in other C Files
the header of the function
being called must be provided as a function prototype within the file from which the function is being
called.
What is a function prototype
consists of just the header of the function, using the same syntax as for a function definition, followed by a semi-colon
also called a declaration of
a function (but not a definition).
Calling Functions in Libraries
you include header file (e.g # include <math .h>/ # include <stdio .h>)
we get access
to the functions defined in the math library. For example, the square root function in the math library
is called sqrt().
compiling programs with a call to a library
-l<library_name></library_name>
for math lib its -lm
How to access library function information on the terminal
Most C libraries are documented with manual pages or, more commonly man pages. These can
be accessed by the UNIX command man
try typing man sqrt on the LINUX command line.
difference between declaration and definition
Function definition: tells the compiler what it does.
it is the actual function ( header and block of code)
A function prototype or declaration: tells the compiler only that such a function is defined somewhere
else without actually defining it.
Function definition: actually allocates storage for the compiled code associated with the function.
What happens every time a function is called
each time a function is called, a
separate copy of the function’s parameters and local variables are created.
this information is stored on a stack
stack frame
The collection of information on the stack for a
a single function call is called a stack frame.
properties of a stack
stack grows from “high”
(greater address) memory locations to “lower”(smaller address) memory locations
how stacks work
information for the main function is loaded in the stack in a stack frame. When it calls another function all the information for that function is pushed onto the stack in another frame. When function is return, the stack frame is popped from the stack. Leaving the calling function on the top of the stack.
The CPU state is restored from the data in the stack frame for the calling function (now
the top stack frame), and execution is resumed at the return address stored there.