F
F
Firetheestle2016-04-09 09:54:47
C++ / C#
Firetheestle, 2016-04-09 09:54:47

How to pass the value of a variable from one function to another?

Good day people! ;з Please tell me how to pass the value of a variable from one function to another? Googled two pairs and ended up not being able to do it ;C
Here is the code

#include<stdio.h>
#include<string.h>
VVOD (x,y)
char x[],y[];
{
  int m,i,j,l,n1;
  puts("Vvedite text");
  gets(x);
  n1=strlen(x);
  for(i=0,j=0,l=0;l<=n1;l++)
  {
    if(x[l]==' '||x[l]=='\0')
    {
      *(y+i*15+j)='\0';
      i++;
      j=0;
    }
    else 
    {
      *(y+i*15+j)=x[l];
      j++;	
    }
  }
  m=i;
  return m;
}

//zadanie1
ZADANIE1(x,y)
char x[],y[];
{
  int k,l,i,j;
  int &n=m;
  l=0;
  for(i=0;i<n;i++)		
    {
      k=strlen(y[i]);
      for(j=0;j<n;j++)		
      {
        if(*(y+i*15+j)>='0'&&*(y+i*15+j)<='9')	
        {
          l=1;
        }
      }
    }
  if(l==1)		
  {
    printf("Cifri Ect'\n");
  }
  else		
  {
    printf("Cifr Het\n");
  }
}

code inserted without main.
The bottom line is to transfer the value of the variable m from the VVOD function to the ZADANIE1 function. Googling, I tried to do it through the link, but it didn’t work out ;[ I also tried through returning the value of the function, but unfortunately it didn’t work out ;C

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
abcd0x00, 2016-04-10
@Firetheestle

A function takes something and returns something. This is the only way functions should communicate with each other. What one function returns must be passed as an argument to another function.

Example
#include <stdio.h>

int func1()
{
    return 5;
}

int func2(int x)
{
    return x * 2;
}

int func3(int x)
{
    return x * 3 + 1;
}

int main(void)
{
    printf("%d\n", func3(func2(func1())));
    return 0;
}

[[email protected] c]$ .ansi t.c -o t
[[email protected] c]$ ./t
31
[[email protected] c]$

Z
zed, 2016-04-09
@zedxxx

Let me quote the answer from here: How many ways are there to pass arguments to a function?

when passing by value, the passed value does not change, and a temporary variable is created inside the function.
when passing by pointer or reference, only the address is passed, the passed object can be changed from inside the function and is not created as a local (temporary) variable inside the function. the difference between passing a reference and a pointer is in usability (no need to write an asterisk every time inside a function).

#include <iostream>

using namespace std;

void f_value(int a){ // значение
    a = 10; // изменяем параметр
}

void f_poiner(int * a){ // указатель
    *a = 10; // изменяем параметр
}

void f_reference(int & a){ // ссылка
    a = 20; // изменяем параметр
}

int main(){
    int a = 0;  
    cout << "a = " << a << endl;    
    f_value(a); // значение переменной (а) НЕ изменится после выполнения
    cout << "a = " << a << endl;    
    f_poiner(&a); // значение переменной (а) изменится после выполнения 
    cout << "a = " << a << endl;
    f_reference(a); // значение переменной (а) изменится после выполнения
    cout << "a = " << a << endl;

    return 0;
}

---------------------------------------------------

a = 0
a = 0
a = 10
a = 20
Press <RETURN> to close this window...

N
Nikita Zubkov, 2016-04-09
@dedi

Variables that you declare in a function are local, and only the function itself "sees" them.
You return the variable m at the end of the first function, so you can save it to another variable in the main. (main code could also be inserted, at least partially)
however, variables declared in main are also local, and only main sees them.
so the variable either needs to be passed as an argument to the function, or a global variable needs to be created.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question