I know this question sound stupid but please answer it.
Let us say we have int A(int num); as a function prototype.
What would we refer to as the name of the function? int A(int num) or A?
Also does the body of the function the part at the top of the definition?
int A(int num){ <-- Does the body include this?
return num * num;
}
Thank you.
Normally you'd go with just "A" as the name of the function; as you said, int A(int num) is its prototype; the body is what the function does, so here just return (num * num);. When referencing a function in a language that allows overloads, you might use the prototype instead of the name, however.
The name of int A(int num) is A.
The first part, int, describe the return type of the function, then we have the name of the function, A, and finally a list of parameters the function accepts (int num). In this case it is a single parameter of type int
The body of a function, is the code which actually performs the functionality of the function. Basically its the code within the curly brackets: { }
int A(int num){
return(num * num); //this is the body
}
Related
Say I had a program where the user could select a number between 0-10. Each number would then correspond to the calling of a certain function. In Python, I know I could just create an array of function names, index into it with the selected option, and then call the function. How would I implement this in C? Or is it even possible?
Here is an example how to do it. Please note that all functions must have the same signature, but of course you can change that from my funptr type to for example a function that has void return or takes a char and not two ints.
// Declare the type of function pointers.
// Here a function that takes two ints and returns an int.
typedef int (*funptr)(int, int);
// These are the two functions that shall be callable.
int f1(int a, int b) { return a + b; }
int f2(int a, int b) { return a - b; }
// The array with all the functions.
funptr functions[] = {
f1,
f2,
};
// The caller.
int call(int i, int a, int b)
{
return functions[i](a, b);
}
The only problem that I can see in the solution from above is that there is no check for the array index (you may get some tricky problems).
To make the code more robust, you can add a check for the index (boundaries), like
add one if statement inside of function "call" where you check the parameter i (not to be bigger than the maximum value)
#include <stdio.h>
int add2nums( int, int);
void main(void)
{
int y,a,b;
printf("Enter 2 numbers\n");
scanf("%d%d", &a, &b);
y = add2nums(a,b);
printf("a is %d\n", a);
printf("b is %d\n", b);
printf("y is %d\n", y);
}
int add2nums( int num1, int num2)
{
int sum;
sum = num1 + num2;
return(sum);
}
So usually, when I create new functions in C the definition of the function is created before the main() function.
In my lecture, there is this example of how to create a function prototype and how they are created by declaring it before the main() function and then defining it after the end of the main() function.
When running the program above, the following error comes up:
Line5: warning: return type of 'main' is not 'int' [-Wmain]|
What is happening? And why is the declaration of the function add2nums() occur twice once before main() and with no parameters?
int add2nums( int, int);
and then again after the end of main() with parameters num1 and num2
int add2nums( int num1, int num2)
There are two valid signatures for the main() function:
int main( void )
int main( int argc, char *argv[] )
Notice that both valid signatures have a return type of int. Any other return type, such as void is not valid and causes the compiler to output a warning message.
When the code calls a function, the compiler needs to know the signature of that called function. There are two ways to tell the compiler what the signature of the called function is:
have the whole function listed before where it is called
have a prototype (aka forward reference) of the function signature before where the function is called. In a prototype the compiler only needs the returned type and the types of the parameters. However, it is a good programming practice to have the names of the parameters listed in the prototype as a courtesy to the humans reading the code.
In C language, the main function has a signature int main(), which lets you to return a value back to the OS, but you made the main() with no void return type. Because of that you've got the warning.
int add2nums( int, int); is just a declaration of the function which lets the compiler make a reference to the function. That's because the compiler reads the file only once.
Names of the parameters aren't required, because they aren't part of the signature.
Declaration of the function is the information for the compiler (not linker as another answer states) - what type the parameters of the function are and what type of the return value is.
So the names of the parameters are not needed.
We can provide an object as an initializer in another object's definition. It just copies and assigns the value of an object to another.
Is it possible to provide a function name as an initializer in another function's definition?
Where does the C standard say if it is allowed or not? I didn't find it, so I suppose it is possible.
The following two examples will result in syntax error:
#include <stdio.h>
int f(){};
int main()
{
int (g=f)();
int h() = f;
}
Thanks.
You probably want a function pointer (as a function doesn't really make sense in this context):
int (*h)() = f;
No. Function declarations can't have an initializer.
If you want to ensure type compatibility, you can use a function typedef.
typedef int f_tp(void);
f_tp f, h; //check f and h for compatibility with int (*)(void)
int f(){} //✓
//will get an error here because h was declared f_tp h == int h(void);
void h()
{
}
How can I create a function pointer to a function where some parameters are set to be fixed upon the definition.
Here is an example what I mean:
Let's say I have the function
int add (int n, int m) {
return n+m;
}
and the function pointer type
typedef int (*increaser)(int);
What I want is a pointer to the function add which fixes the first parameter to 1 and leaves the second paramenter open. Something along the lines of
increaser f = &add(1,x);
How can I accomplish this?
What I want is a pointer to the function add which fixes the first parameter to 1 and leaves the second paramenter open.
There is no such thing in C. The closest you can come is to create a wrapper function and make a pointer to that:
int add1(int x) {
return add(1, x);
}
increaser f = &add1;
If you don't need a pointer to the function then you can use a macro:
#define increaser(x) add(1, (x))
C does not support doing this directly. In C++ there is std::function and bind which can achieve this however.
None the less for a pure C solution the closest you can reasonably get is defining a new function that calls add like:
int increment(int input) {
return add(1, input);
}
Then you can do:
increaser f = &increment;
I want to pass a float number to another function.
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number)
}
fun(float star, float ste, int numbe)
{
//here I get "star = 0", "numbe = 10", but "ste = -1.084264e-19"
}
what is wrong here?
Thank you
It is most certainly possible to pass a float to a function, as long as you provide a prototype or move the definition ahead of the first use of the function.
A prototype (also called a forward declaration) looks like this:
void fun(float star, float ste, int number);
In larger projects, prototypes go into header files.
Note: do not forget to add void in front of the function definition as well. Otherwise, the compiler treats your function as returning an int.
If you omit the prototype, the compiler will default to using the old K&R C rules to decide how to pass arguments to your function. The result is the unusual behaviour you are seeing, and hopefully a compiler warning as well.
You need a correct function declaration before your function call:
Add:
void fun(float star, float ste, int numbe);
before your main declaration and also add void return type in your fun function definition.
you need to declare the function before main function.
Try this:
void fun(float star, float ste, int number);
int main()
{
float start=0;
float step=0.1;
int number=10;
fun(start,step,number);
}