As my topic says, I want to know a method to send a value to a function which is called by another function. For example:
int main(){
int sendingvalue=funtionreturningvalue();
int x=0;
function1(x);
}
function1(int x){
//some code here
function2(x);
}
function2(int y){
//again some code here
function3();
}
function3(){
//here I need the top value sendingvalue
}
It is so useless to explicitly pass that int sendingvalue again and again in all the functions until it reaches function3. So what is the best option to pass this value?
There are 2 options. You either make it a global variable, or you pass it through each function.
To make it a global variable, add the following line outside all of your function definitions:
int sendingvalue;
Then you access modify and read it from any function. If you don't want to use a global variable, you'll have to pass it along the stack.
Related
in C I am writing some of my very first exercises. Earlier on, I tried to declare a simple function inside of main and it comes with an error: "function definition is not allowed here". But I thought a function could be declared inside of main or outside, the only difference being the scope?? I have also read, in here, of other people writing functions inside of main, so why won't it let me do it?
thanks
You can declare a function inside another function:
int main(void) {
int foo(int); // declaration
...
}
But you can't define a function inside another function:
int main(void) {
// Doesn't work.
int foo(int x) {
return x * 2;
}
...
}
Also, declaring functions inside other functions is a really unusual thing to do, and essentially never necessary.
void foo() {
int i = 3;
bar();
}
void bar() {
//print i in foo()
}
I was wondering if there is some way that I could do this without setting a global variable?
No, this is a local variable and is stored in the stack for that method, outside that method the stack does not exist and can not be read
If you want to just use the value of i, you can use:
void bar(int i);
void foo() {
int i = 3;
bar(i);
}
void bar(int i) {
//print i in foo()
}
If you want to be able modify the value of i, use:
void bar(int* ip);
void foo() {
int i = 3;
bar(&i);
}
void bar(int* ip) {
*ip = 20;
}
C uses static scoping. You can check this here: https://msujaws.wordpress.com/2011/05/03/static-vs-dynamic-scoping/ :)
Simply pass i from foo to bar as parameter:
bar(i);
void bar(int i)
You can, but you have to pass it to foo as an argument. The reason for this is that when a C function is called it creates any local variables defined inside it on the stack, which is essentially just a form of dynamic memory allocation. When the function returns, it frees the memory used by those variable. Now you might think there should be some way for bar to know about i since it is still present on the stack when bar is called inside foo. But the problem is that C does not make any assumptions about who might be calling the bar function. You could just as easily call bar from a third function that has no i defined within it. For this reason there is no mechanism for accessing stack variables from a nested function without passing them explicitly.
Interestingly, having the ability to do this built into the language is one of the key features that allows for the implementation of closures. So if you have ever struggled to understand what made these so special, this is basically it. Probably not as spectacular as the hype might have lead you to believe.
I need to figure out how to use a void type function to change a value in another function, so I'm trying to write a practice program that uses a procedure to change an integer from 5 to 4 and then prints the new integer (should be 4).
#include <stdio.h>
#include <stdlib.h>
void change(int x)
{
x = 4;
}
int main(int argc, char **argv)
{
int z = 5;
change(z);
printf("%d\n",z);
return 0;
}
This prints 5 at the end. I can tell there's some kind of issue with scope here, but I can't figure out how to resolve it. I also can't print within the procedure, so that solution is out of the question. I'd really appreciate any help!
To change a variable within another function, that isn't in the scope of the function, you must pass the variable by pointer.
void change(int *x)
{
*x = 4;
}
And call the function using change(&z).
If the variable isn't passed by pointer, then only the variable inside the scope of the function will change, but not its argument.
In C, function arguments are always passed by value. This means that any changes made to a value in a function are not reflected in the caller. That is what's happening in your case.
Fortunately, you can pass a pointer (by value of course) instead. This allows you, via dereferencing, to change the value that the pointer is pointing to.
To do this, adjust the prototype of your function to
void change(int* x)
Then, within that function, use
*x = 4;
And, finally, call the function using
change(&z);
You need to pass the address of the variable and then you can change the value of the variable.
void change(int *x)
{
*x = 4;
}
Now the invoking function will have new value of x which is 4.
You can pass a pointer to the function, like so:
void change(int *x)
I'm trying to understand the behavior of pointers in C in context of passing them as arguments to function, so I tried messing around and making the following test case:
void function1(int argument)
{
argument=4;
}
and
void function2(int* argument)
{
*argument=5;
}
if I run the following statements:
int var1;
int* var2;
function1(var1);
function1(*var2);
function2(&var1);
function2(var2);
In which cases will changes made to the variables be reflected in the calling function?
I tried running the following sample code but am not able to understand output
#include <stdio.h>
void funone(int arg)
{
arg=5;
}
void funtwo(int* arg)
{
*arg=6;
}
int main(void) {
int var1=0;
int *var2;
var2=(int *)malloc(sizeof(int));
*var2=0;
funone(var1);
printf("%d",var1);
funone(*var2);
printf("%d",*var2);
funtwo(&var1);
printf("%d",var1);
funtwo(var2);
printf("%d",*var2);
return 0;
}
the output I'm getting is
0066
what is the implication of this output?
Your code for function1 changes the value of the variable in the function only. So there are no repercussions of the change outside of the function.
Your function2 works (ie, changes the values it points to) because pointers can point to variables outside of it's scope. But your first function keeps the value inside of it's scope, and once the function returns any changes are lost.
This explanation should be present in any good tutorial about pointers.
Neither of those examples will work as you're simply setting the value of the pointer using those functions. Below you will find a version which achieves the behaviour you wish:
void function2(int* argument)
{
*argument=5;
}
The reason for the asterisk before the argument parameter is because you are dereferencing the value of the pointer (you are getting access to it) and therefore you may, at that point change its value...
I hope this helps...
I have a function in C that I want to output four different values, so rather than using return in my function I decided have four different variables as arguments to the function that would carry their values out of the function back into my main code. I figured if I defined the variables in main and fed them to my other function, they would have whatever value the function gave them after exiting the function. This does not happen though. The variables end up having a value of 0 or close to 0 (like, around 10^-310).
Do I have to declare my variables in a different way/with a different scope to allow them to keep the values they had in a function after exiting the function? Or is there a way to return multiple values in a function?
Here's an excerpt of the relative code:
void PeakShift_FWHM_Finder(double fwhml,double fwhmr,double peak, double max)
{
...//stuff happens to these variables
}
int main()
{
double fwhml,fwhmr,peak,max;
...//other stuff to other variables
PeakShift_FWHM_Finder(fwhml,fwhmr,peak,max)
//These four variables have the right values inside the function
//but once they leave the function they do not keep those values.
...//code continues...
return 0;
}
Use pointers instead.
void PeakShift_FWHM_Finder(double *fwhml,double *fwhmr,double *peak, double *max)
{
...//stuff happens to these variables
// REMEMBER TO DEAL WITH (*var_name) INSTEAD OF var_name!
}
int main()
{
double fwhml,fwhmr,peak,max;
...//other stuff to other variables
PeakShift_FWHM_Finder(&fwhml,&fwhmr,&peak,&max)
//These four variables have the right values inside the function
//but once they leave the function they do not keep those values.
...//code continues...
return 0;
}
What you are looking for is something called Passing by reference
To achieve that, you need to change your declaration to take pointers to the variables. For example
void foo(int * x) {
(*x)++;
}
Then, you can simply invoke that function passing values to it through their address.
int main() {
int i = 10;
foo(&i);
printf("%d", i);
}
What this does is passes the address location of the variable to be modified and the function directly modifies the variable at that address.