Functions in C...

This is a discussion on Functions in C... within the C Programming forums, part of the General Programming Boards category; Hey peeps.... having a little trouble with functions. I have a couple of good books but they go onto strings ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    44

    Functions in C...

    Hey peeps.... having a little trouble with functions. I have a couple of good books but they go onto strings and strctures and other wierd stuff before they move to functions and I get confused looking at the code.... What I want to ask is:

    How do you define a function in your main prog?

    When you call a program does the main program give info to it, or does it take info?

    Same as above for argument.

    How do you call a function?

    Where do you place the function in your code? Before or after you start your program? Does it matter?

    And finally, is this the right way to start a function?

    ret_type functname(argument)

    TIA... I read the tutorial but I don't really get it...

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    How do you define a function in your main prog?
    You declare it just as you stated below:
    ret_type functname(argument); This is the function prototype.

    When you call a program does the main program give info to it, or does it take info?
    Not sure what you're trying to ask here.

    How do you call a function?
    You just call your function w/ your arguments. As long as your function has been defined before it is called just like using a variable.

    Where do you place the function in your code? Before or after you start your program? Does it matter?
    You can place the function before or after main depending on your preferences. Usually if you place it before, you do not have to define the function prototype. However, you must order your functions in a manner such that you define them before you call them. Personally, I place my functions after main and therefore, forcing myself to define them above the main.

    And finally, is this the right way to start a function?
    ret_type functname(argument)
    {
    // place code here
    }

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Functions in C...

    >When you call a program does the main program give info to it, or does it take info? Same as above for argument.
    Yes. Or no. It's up to the programmer.

    >How do you call a function?
    By specifying it's name in the correct syntax, together with args, and somewhere to store the returned value if need be. See example.

    >Where do you place the function in your code? Before or after you start your program?
    >Does it matter?
    Doesn't really matter, as long as they are "prototyped" before they are used. See example.


    >And finally, is this the right way to start a function?
    Well, there are plenty of wrong things to do, but this:
    >ret_type functname(argument)
    is basically correct.

    Code:
    #include <stdio.h>
    
    int f(int); /* function prototype */
    
    int main(void)
    {
    	int i;
    
    	/* Now call f, passing it a value of 10,
    	 * storing the result in i
    	 */	
    	i = f (10);
    	
    	/* print the value of i */
    	printf ("i is %d\n", i);
    	return 0;
    	
    }
    
    /* Function f.  Returns the my_i + 10 */
    int f(int my_i)
    {
    	my_i = my_i + 10;
    	return (my_i);
    }
    [edit]darn, beaten to it..
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Thanks a lot, to you both...

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Sorry to bother you all again... I keep on forgetting everything-and sometimes I just can't understand and keep on getting confused!

    How do I call a function that has a ret type and takes an argument of ints? Let's call the ints x, and y. Please just give me an example...

    How do I pass a value (like ints or floats) to a function? How does it work? Can someone give me an example?

    And now for my damned homework. Need to create a program that takes two values, multiplies them, then takes the product and asks you to put in another number to multiply your product with, until you tell it to stop. Here's my attempt so far... I have my idea of how to do it but my functions and all that is a mess... I also put in some unfinished gotos so don't bother with those... but how can I implement my function where I want to?

    Code:
    float calc(float);
    void main()
    
    {
    
    	float x, y, z, prod1, prod2, prod_main, res;
    	int optn1, input;
    	printf("Please enter two values here");
    	scanf("%f %f", &x &y);
    	prod1=x*y;
    	printf("\nYour product is %f . If you would like to continue, press "1" key on your keyboard.", prod1);
    	printf("\nIf you want to stop, press the "2" key on your keyboard.");
    	scanf(" %d ", &input);
    
    		
    		switch(input)
    	
    		{
    
    			case 1: calc();
    			break;
    
    			case 2: //goto(end of program)
    			
    
    			default: printf("\nInvalid Choice! Please pick either 1 or 2!");
    			
    
    		}
    
    
    	
    	printf("Please enter a number to multiply your sum with");
    	scanf(" %f ", &z);
    
    	//call program, PASS z and prod1 to function, function returns prod_main
    	calc(z, prod1);
    	//call program, get prod_main from function
    
    	prod_main=res;
    
    	printf("Your product is %f .If you would like to continue, press "1" key on your keyboard.", res);
    	printf("\nIf you want to stop, press the "2" key on your keyboard.");
    
    
    	prod1=ret_val;
    
    
    	goto(the printf that tells you your product);
    
    	//specify this place for the switch-case goto
    
    	
    
    	getch();
    
    }
    	
    //Let's declare our program
    
    float calc(float cvar1, cvar2);
    
       {
    	float ret_val;
    	ret_val=cvar1*cvar2
    
    	return(ret_val);
    
       }

    I haven't tried to compile it yet-and I also wanted to know: If the float I defined as ret_val is in the function, will it only work in the function program? Not in my main program? Please help... I'm confused to hell on this one and I can't take it nomore.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Update... I can't the program to compile- here's the error.

    calcprog.c(42): E2226 Extra parameter in call to calc

    It's where I have prod_main=calc(prod1, z);

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    float calc(float);
    void main()
    
    {
    
    	float x, y, z, prod1, prod2, prod_main, res;
    	int optn1, input;
    	printf("Please enter two values here, seperated by spaces:  ");
    	scanf("%f %f", &x, &y);
    	prod1=x*y;
     	printf("\nYour product is %f .", prod1);
            printf("If you would like to continue,\n press the 1 key on your keyboard.");
    	printf("\nIf you want to stop, press the 2 key on your keyboard.");
            
    	scanf("%d", &input);
    
    		
            cloop: switch(input)
    	
    		{
    
    			case 1: goto functn;
    			break;
    
    			case 2: goto endprog;
    			
    
    			default: printf("\nInvalid Choice! Please pick either 1 or 2!");
    			         goto cloop;
    
    		}
    
                      clrscr();
    	
    functn:	printf("Please enter a number to multiply your sum with");
    	scanf(" %f ", &z);
    
    
    	prod_main=calc(prod1, z);	//PASS z and prod1 to function, function returns  to  prod_main
                            //"ret_val" int RETURNED from CALC to PROD_MAIN. Yeah.
    	
    
    	printf("\nYour product is %f .", prod_main);
            printf("If you would like to continue,\n press the 1 key on your keyboard.");
    	printf("\nIf you want to stop, press the 2 key on your keyboard.");
    
    
    
    
    	goto cloop;
    
     endprog: printf("Press any key to exit.");
    
     getch();
    
    
    }
    	
    //Let's declare our program
    
    float calc(float cvar1, float cvar2)
    
    
       {
       
    	float ret_val;
    	ret_val=cvar1*8;
    
    	return(ret_val);
    
       }
    Thanks for the help...'ppreciate it

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by diddy02
    How do I call a function that has a ret type and takes an argument of ints? Let's call the ints x, and y. Please just give me an example...

    How do I pass a value (like ints or floats) to a function? How does it work? Can someone give me an example?
    Already done. See my post above.

    With regards to your code:
    >void main()
    is wrong, main returns an int. Make it
    >int main(void)

    >goto
    goto is a statement that is not used often in C. A lot of people hate it It does have it's good uses, but in my opinion, it's something a newbie should learn after learning flow control through the more conventional methods (if statement, loops etc).

    >I haven't tried to compile it yet ....
    Well, it's about time you did. If you are new to coding, write a small amount first, and compile frequently. This way you don't get yourself into so much of a mess.

    >If the float I defined as ret_val is in the function, will it only work in the function program? Not in my main program?
    The variable ret_val is only available within the function it's defined in. But its value is returned back to the caller, and therefore that value is assigned to a local variable within main.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Hammer, you sent the value of ten to f... I want two send to vars, like example:

    float calc(x, y);

    When I do that I get an error in the compiler... but it works fine with one... so I how do I send two values to the function?

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    44
    Forget it, figured it out... s0rry for botherin yA.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Forget it, figured it out... s0rry for botherin yA.
    No worries, you didn't
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

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