Videos
I am trying to learn C for the first time.
Can someone please explain to me when it is necessary to use the void function? I don't understand what is meant by it doesn't return a value.
If you don't have a forward declaration of your function before the place of usage, the compiler will create implicit declaration for you - with the signature int input(). It will take the name of the function you called, it will assume that the function is returning int, and it can accept any arguments (as Bartek noted in the comment).
For this function, the implicit declaration matches the real declaration, so you don't have problems. However, you should always be careful about this, and you should always prefer forward declarations instead of implicit ones (no matter if they are same or not). So, instead of just having forward declaration of the void prime() function (assuming that you will use it somewhere), you should also have a forward declaration of int input().
To see how can you pass any number of the arguments, consider this:
#include <stdio.h>
// Takes any number of the arguments
int foo();
// Doesn't takes any arguments
int bar(void)
{
printf("Hello from bar()!\n");
return 0;
}
int main()
{
// Both works
// However, this will print junk as you're not pushing
// Any arguments on the stack - but the compiler will assume you are
foo();
// This will print 1, 2, 3
foo(1, 2, 3);
// Works
bar();
// Doesn't work
// bar(1, 2, 3);
return 0;
}
// Definition
int foo(int i, int j, int k)
{
printf("%d %d %d\n", i, j, k);
return 0;
}
So, inside the definition of the function you're describing function arguments. However, declaration of the function is telling the compiler not to do any checks on the parameters.
Not declaring a prototype and relying on default argument/return type promotion is dangerous and was a part of old C. In C99 and onward it is illegal to call a function without first providing a declaration or definition of the function.
my question is, is it necessary to declare a void function with not arguments?
Yes. For this you have to put void in the function parenthesis.
void foo(void);
Declaring a function like
void foo();
means that it can take any number of arguments.
Hello,
I am a beginner at coding and I am currently learning C++ as my first language. I started 3 days ago, and I am using Codecademy to learn. Right now I am doing a subsection "Parameters and Arguments" in the section "Functions." The way I was taught to use the return function is as follows in the code that they gave me:
#include <iostream>
bool morning = true;
std::string make_sandwich(){
std::string sandwich = "";
sandwich += "bread\n";
if (morning == true) {
sandwich += "egg\n;
}
sandwich += "cheese\n";
sandwich += "bread\n";
return sandwich;
}
int main {
std::cout << "Your sandwich now:\n" << make_sandwich();
}This was the previous lesson before I was given an assignment, and I have been trying for a long time to do it. It told me: "Let's build a function that prints out the current emergency number, whatever it is from now on. Above `main()`, define a `void` function `get_emergency_number()` that accepts one parameter: a string with the name `emergency_number`."
There are some other steps, but I cannot proceed until I have finished this one.
The code (without // comments [it is kind of hard to understand the comments because it is also for other steps]) is as follows:
#include <iostream>
int main() {
int emergency_number;
std::string old_emergency_number = "999";
std::string new_emergency_number = "0118 999 991 999 725 3";
}This is my most recent try:
#include <iostream>
void get_emergency_number() {
std::string int emergency_number;
}
int main() {
int emergency_number;
std::string old_emergency_number = "999";
std::string new_emergency_number = "0118 999 991 999 725 3";
}I had many, many more tries, but I could not get this to work, I even had a somewhat-experienced coder look at the code, but they could not figure it out.
Please help me and do not just fix, I would prefer an explanation that will serve good use to me for my knowledge.
Thank you,
Zach
Let's make it more clear first. You have a void function, called mark_function, which takes 2 parameters. The first parameter is a void pointer and the second parameter is a pointer to a function that returns void and takes as a parameter a void pointer. Let's create a function that will be apropiate to pass as parameter to mark_function.
void param(void *p) {
// function body
}
Assume a and b are 2 void pointers. I will not enter into details about their scope, but you need to pay attention to it: they must be available in the scope they are used.
Then, the mark_function will be called as:
mark_function(a, param);
Inside mark_function body you can have something like:
param(b);
, which is a call to the function passed as a parameter.
Long story short: the function pointers used as parameters are meant to make it possible for the function that requires them to perform different activities by calling different functions. The value of a pointer function is simply the name of a function that has the appropiate signature (return value and parameter list). Your function can use this to call the function provided as parameter as needed.
You should call the function with one pointer to an object, and one pointer to a function of suitable type.
int my_int;
void mark_function(void *obj, void (*mark_obj)(void *));
void my_func (void *vp)
{
/* Convert vp to a pointer to a real type */
int *ip = vp;
/* Do something with ip or *ip */
}
mark_function (&my_int, my_func);
A pointer to object (&my_int) can safely be convert to a void * and back to the same type. The resulting pointer is guaranteed to be identical to the original pointer. This allows mark_function() to perform some task regardless of the actual type that the pointer points to.
Typical examples of such tasks include sorting of arrays with a custom compare function provided by the caller, or maintaining linked lists without knowing the types of the object stored inside, an so on.