passing variables between functions

This is a discussion on passing variables between functions within the C Programming forums, part of the General Programming Boards category; Hi all! As a newbie I try to dig into every basic problem to learn the proper way of programming, ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    61

    passing variables between functions

    Hi all!
    As a newbie I try to dig into every basic problem to learn the proper way of programming, instead of make a work-around. My question is: Why is the choice-variable not passed in a proper way? The second time it's printed a very weird number is diplayed
    Code:
    #include <stdio.h>
    
    int Menu(int choice) 
    {	
    	printf("enter a number: ");
    	scanf("%d", &choice);
    	printf ("Choice is: %d\n", choice);
    	return choice;
    } 
    
    int main(void) 
    {
    	int choice;
    	Menu(choice);
    	printf ("Choice is: %d\n", choice);
    	return 0;
    }
    Thanks in advance for answer

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    Try this
    Code:
    #include <stdio.h>
    
    int Menu(void) 
    {	
    	int choice;
    	printf("enter a number: ");
    	scanf("%d", &choice);
    	printf ("Choice is: %d\n", choice);
    	return choice;
    } 
    
    int main(void) 
    {
    	int choice;
    	choice = Menu();
    	printf ("Choice is: %d\n", choice);
    	return 0;
    }
    C doesn't have reference parameters.
    So either return the result, or pass a pointer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thanks! I tried both methods, now with passing the values of two varibales, but get wrong output again:
    returning method:
    Code:
    #include <stdio.h>
    
    int Calc(int x,	int y){
    	int z;
    	int a;
    	scanf("%d", &x);
    	scanf("%d", &y);
    	z=y+x;
    	a=y*x;
    	printf ("z:%d a:%d\n", z,a);
    	return z,a;
    } 
    
    int main(void){
    	int x;
    	int y;
    	int z;
    	int a;
    	z,a=Calc(x,y);
    	printf ("z:%d a:%d\n", z,a);
    	return 0;
    }
    pointer method
    Code:
    #include <stdio.h>
    
    int Calc(int x,	int y, int *zpointer; int *apointer){
    	scanf("%d", &x);
    	scanf("%d", &y);
    	*zpointer=y+x;
    	*apointer=y*x;
    	printf ("z:%d a:%d\n", *zpointer, *apointer);
    } 
    
    int main(void){
    	int x;
    	int y;
    	int z;
    	int a;
    	int*zpointer;
    	int*apointer;
    	*zpointer=z;
    	*apointer=a;
    	Calc(x,y, *zpointer,*apointer);
    	printf ("z:%d a:%d\n", z,a);
    	return 0;
    }

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    1st method.From a function you can only return one "thing" ,variable here,not two!
    For the second, when you pass variables by reference you call them by their addresses,like this
    Code:
    Calc(x,y, &zpointer,&apointer);
    Also you want the pointer to be set at the address of a variable at initialization ,so
    Code:
    *zpointer=z;
    should be
    Code:
    zpointer=&z;

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    I upgraded the code, but it doesn't want to compile:
    Code:
    #include <stdio.h>
    
    int Calc(int x,	int y, int*zpointer; int*apointer){
    	scanf("%d", &x);
    	scanf("%d", &y);
    	&zpointer=y+x;
    	&apointer=y*x;
    	printf ("z:%d a:%d\n", zpointer, apointer);
    	return 0;
    } 
    
    int main(void){
    	int x;
    	int y;
    	int z;
    	int a;
    	int*zpointer;
    	int*apointer;
    	zpointer=&z;
    	apointer=&a;
    	Calc(x,y,&zpointer,&apointer);
    	printf ("z:%d a:%d\n", z,a);
    	return 0;
    }

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by sababa.sababa View Post
    I upgraded the code, but it doesn't want to compile:
    Without having tried to compile your code I would say that it doen't compile because of
    Code:
    Calc(x,y,&zpointer,&apointer);
    zpointer and apointer are already pointers to int.
    Try
    Code:
    Calc(x,y, zpointer, apointer);
    Also In Calc : to dereference a pointer use the * operator ( not & ).
    Kurt

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    I've tried different ways but only this work with my gcc-compiler:
    Code:
    #include <stdio.h>
    
    int Input(){
    	int something;
    	scanf("%d", &something);
    	return something;
    } 
    
    int Calc1(int x, int y){
    	int z;
    	z=y+x;
    	return z;
    } 
    
    int Calc2(int x, int y){
    	int a;
    	a=y*x;
    	return a;
    } 
    
    int main(void){
    	int x;
    	int y;
    	int z;
    	int a;
    	x=Input();
    	y=Input();
    	z=Calc1(x,y);
    	a=Calc2(x,y);
    	printf ("z:%d a:%d\n", z,a);
    	return 0;
    }
    I suppose that's the proper way of passing variables? My deduction is that in general a function should only return one variable. Am I right?

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Yes ,only one variable.That variable may be many things though somehow set in a collection (e.g. structs,arrays..)

    Notice that when a function has no paramaters, it is a good style to write
    Code:
    int Input(void)
    Actually when you do not specify the word void, you might get yourself in logical errors.

    Also notice that it is elegant to use prototypes of functions before main and the definition of them after it.In code
    Code:
    int Calc2(int x, int y);
    
    int main(void)
    {
         ...
    }
    
     
    int Calc2(int x, int y){
        int a;
        a=y*x;
        return a;
    }

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Thanks a lot!

    In order to save RAM memory I suppose one should use malloc. Is that a standard approach for dealing with values? When should one use regular variables and when should one use malloc/free? Does malloc/free use more CPU than regular variables?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int Input(void);
    
    int Calc1(int *x, int *y);
    
    int Calc2(int *x, int *y);
    
    int main(void){
            int *x = malloc(sizeof(int));
            int *y = malloc(sizeof(int));
            *x=Input();
            *y=Input();
            int *z = malloc(sizeof(int));
            int *a = malloc(sizeof(int));
            *z=Calc1(x,y);
            *a=Calc2(x,y);
            free(x);
            free(y);
            printf ("z:%d a:%d\n", *z,*a);
            free(z);
            free(a);
            return 0;
    }
    
    int Input(void){
            int something;
            scanf("%d", &something);
            return something;
    }
    
    int Calc1(int *x, int *y){
            int z;
            int y2 = *y;
            int x2 = *x;
            z=y2+x2;
            return z;
    }
    
    int Calc2(int *x, int *y){
            int a;
            int y2 = *y;
            int x2 = *x;
            a=y2*x2;
            return a;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with passing variables to different functions
    By Draylath in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2012, 01:22 PM
  2. Passing variables to functions
    By theitsmith in forum C Programming
    Replies: 4
    Last Post: 02-06-2012, 11:19 AM
  3. passing variables between functions
    By owi_just in forum C Programming
    Replies: 6
    Last Post: 05-08-2005, 09:12 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. passing variables to functions?
    By aoe in forum C Programming
    Replies: 12
    Last Post: 06-02-2002, 04:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21