I'm trying to call other adjacent functions (delay() and delay_ex()) from a function (bitcheck()) as shown below and as expected, the compiler has thrown an error that delay() and delay_ex() functions weren't declared in the scope and I understood that we can't call functions other than from the main. So, I declared these delay() and delay_ex() functions in a header file and called from this program by including the header file, it worked well. So, is there any other such way to make this work?
void bitcheck()
{
int i;
for(i=0;i<NELEMS(array); i++)
{
delay();
AP_CMU->DIVIDER = freq_def[0];
encryption(array,i);
delay();
// LCD_DisplayUint32(i,0,array[i]);
AP_CMU->DIVIDER = freq_def[6];
delay_ex(10);
decryption(intr_array,i);
delay_ex(10);
// LCD_DisplayUint32(i,10,array[i]);
}
}
void delay()
{
int i;
for (i = 0; i < 100000; i++) {
__NOP();
}
}
void delay_ex(int j)
{
for(int s=0; s < j; s++)
{
for ( int i = 0; i < 10000; i++) {
__NOP();
}
}
}
You can write your functions above the code that calls them like:
void foo() {
}
void bar() {
}
int main () {
foo();
bar();
}
You can also forward declare functions like:
void foo();
void bar();
int main () {
foo();
bar();
}
void foo() {
}
void bar() {
}
Or you can put them in a header file and include it
file.h:
void foo();
void bar();
file.c:
#include "file.h"
int main () {
foo();
bar();
}
void foo() {
}
void bar() {
}
The compiler works in a single pass, as such when bitcheck() is parsed, the signatures of delay() and delay_ex() are not known, so the compiler cannot verify the call is type-correct.
The rule is declare or define before use; there are two possible solutions:
Define bitcheck() after the definition of delay() and delay_ex()
Forward-declare delay() and delay_ex().
By declaring the functions in a header and including it before defining bitcheck(), you used the second of these solutions, but use of an include file was not essential - #include does noting more than insert the file content into the translation-unit prior to compilation. This is useful when the symbols will be called from a different translation-unit than that in which they are defined; if that is not intended the declarations may be written directly rather then #include'd, and should also be declared static to avoid external-linkage and potential name clashes with other translation-units.
You need to define delay and delay_ex before bitcheck. Simply moving those two functions above bitcheck should suffice.
Related
Let's assume I have 2 functions other than the main(), respectively func1() and func2(). Is it possible for me to call func1() in func(2) without declaring it first? Or should I use a pointer to the other function and pass it as an argument? Thanks in advance.
_"Is it possible for me to call func1() in func(2) without declaring it first?"_
It depends on compiler, but generally this will not work. (see exclusion example at bottom of answer.)
Some scenarios that illustrate:
Scenario 1: Normally will not work as function is being referenced before being declared:
int main(void)
{
int ret = func1();
return 0;
}
int func1(void)
{
return 1;
}
int func2(void)
{
return func1();
}
Results:
9, 15 error: implicit declaration of function 'func1' is invalid
in C99. Make sure that you include the function prototype.
Scenario 2: As all required definitions occur in order, this will compile and run without issue:
char func1(void)
{
return 1;
}
char func2(void)
{
return func1();
}
int main(void)
{
char ret = func1();
ret = func2();
return 0;
}
Results:
Compiles and runs with no problem because both functions are defined before being called (both func2() called from main() and func1() called from func1)
Scenario 3: The best way is always to pre-declare functions using prototypes either in same file before functions are called or in a header file that is #included in any source file that uses them. This clears up any potential problems, especially for those that inherit the code for maintenance:
int func1(void);
int func2(void);
int main(void)
{
int ret = func1();
return 0;
}
int func1(void)
{
return 1;
}
int func2(void)
{
return func1();
}
Regarding your comment:
"...a statement in my book caused confusion, I thought it might be related to a difference of the version of the C compiler in the book and I am using."
Could be: Per comment below, pre-standard C function definitions are supported by some modern compilers (eg gcc) thus would compile scenarios 1 & 2 without issue if functions complied with the default function definition; eg:
int func1(void)
int func2(void)
Here is how you do it:
void func2(int code); // forward declaration
void func1(const char* str)
{
func2(str[0]); // a call to a declared function
}
void func2(int code) // the callee
{
printf("code: %d\n", code);
}
I have a simple program like:
int velocity=0;
#include "extra.h"
int main()
{
extra();
return 0;
}
where extra.h is:
void extra(){
velocity += 1;
}
yet when I compile this, I get the error:
extra.h:5:5: error: 'velocity' was not declared in this scope
Obviously, my code in extra.h can't "see" the variables in main.c, but why is this? How do I fix this?
You could add the following declaration to extra.h:
extern int velocity;
However, extra() shouldn't be defined in extra.h in the first place. This will cause problems if extra.h is included by multiple .c files in the same binary. The following is what you should have:
extra.h:
void extra();
extra.c:
#include "extra.h"
static int velocity = 0;
void extra() {
velocity += 1;
}
main.c:
#include "extra.h"
int main()
{
extra();
return 0;
}
Consider following source file:
#include <stdio.h>
int g_var_1;
int g_var_2;
// ...
void f1(void);
void f2(void);
// ...
int main(void)
{
f1();
f2();
return 0;
}
void f1(void)
{
// set the value of g_var_1
g_var_1 = 100;
}
void f2(void)
{
// read the value of g_var_1
printf("%d\n", g_var_1);
}
// ...
Is it possible to "apply promise" for some functions (within the same translation unit) that g_var_1 should be considered as read-only global variable for them? I have tried with something like:
void f2(void)
{
extern const int g_var_1;
// read the value of g_var_1
printf("%d\n", g_var_1);
}
but this yields into:
error: conflicting type qualifiers for ‘g_var_1’ extern const int
g_var_1;
Essentially I would like to restrict possibility of unintended modification of global variable in more complex code (values for real are known compile-time), still without "hooking" them as functions' paramaters, like as:
void f2(const int g_some)
{
printf("%d\n", g_some);
}
The appropriate way to do that is by separating your code into modules, and defining those globals as static, which will make them only module visible and will not export their symbol outside. You can then add a getter function to expose their value, without exposing them to modifications from outside the module.
This is the program
void message();
int main() {
message();
printf("Cry,and stop the monotony");
return 0;
}
void message() {
printf("smile and see");
}
In this program message function is called in main() but it is defined after function main so if the program execution begins from top side then it should generate error but it still works?how? can someone explain me?
second one can I write this program like this:
void message() {
printf("smile and see");
}
int main() {
message();
printf("Cry,and stop the monotony");
return 0;
}
It can be compiled because the declaration
void message();
tells to the compiler that such a function exists and is defined somewhere. It is so called forward declaration. The compiler then finds it after main function.
Forward declaration is used sometimes to declare that some entity exists somewhere and is defined but not yet, here you state just that it exists and you would like to use it. In case of class forward declaration you can use a pointer and reference to class objects without including class definition. For example
file.h
class Something;
// not included but I can use a pointer or reference
// as long as I don't call methods on it
void function( Something const& s);
file.cpp
#include "Something.h" // now it is needed because I will call a function on s
#include "file.h"
void function( Something const& s) {
s.sing();
}
The first line
void message();
is a forward declaration. That tells the compiler that an implementation of that function exists and follows.
void message(); // This is the function declaration
int main(){
message(); // Calls message()
printf("Cry,and stop the monotomy"); // Prints this phrase
return 0;
}
void message(){ // This is the function definition
printf("smile and see");
}
That's all there is to this example.
NO this is unconventional:
void message() {
printf("smile and see");
}
int main(){
message();
printf("Cry,and stopthemonotomy");
return 0;
}
As per your First Code
void message();
int main() {
message();
printf("Cry,and stop the monotomy");
return 0;
}
void message() {
printf("smile and see");
}
The c++ compiler works from Top to Bottom Approach so it will first find the statement
void Message(); as this is a declaration of function so it treat it as a Forward Declaration.
and Search for the definition in remaining code.
And About the Second Code Block:
Compiler itself find the definition of function at the first line void message(){...}
So it treat as a declaration and Definition.
I was trying to implement access specifier (not sure if that is called access specifier)
The purpose is to make a function (func2) callable only at one place(inside func1).
int func1 ()
{
// Make func2 callable only here`
#define FUNC2_CALLABLE
func2();
#undef FUNC2_CALLABLE
}
#ifdef FUNC2_CALLABLE
int func2 ()
{
return 1;
}
#endif // FUNC2_CALLABLE
func2 should be callable only from func1 and not from any other place in the code.
Does the above code serve the purpose ? Any alternative suggestions
< Edit 1 >
How about doing it this way
int func2()
{
#ifdef FUNC2_CALLABLE
return 1;
#endif
}
Will this work ?
< / Edit 1>
That will give you a linker error of func2 not found (func2 won't be defined if you use that).
I think you might be looking for static.
static int fake(int x) { return x * 2; }
int doSomething(int x) { int tmp = fake(x); doOtherThings(); }
The fake would not exist outside of the compilation unit (file basically).
As for allowing a function to only be called from another function, that doesn't make much sense. Just inline the code if that's your end goal.
There is no real way to do it. The best that can be done in standard C is to make func2 static and define it close to the bottom of the source file:
static int func2 ()
{
return 1;
}
int func1 ()
{
func2();
}
(end of file)
Maybe you can use static keyword .
But the function with static is accessible for all the code in the same file. Other files cannot access it.