GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Mar-2004, 18:48
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

Understanding functions


Hello everyone,

I do not understand functions at all. I have read a lot of tutorials and I am still unclear as to exactly how functions work. I know what functions are, that they are a block of code that saves the programmer a lot of time so they dont have to rewrite repetitive code. Functions perform 1 task and returns 1 value, actually it's possible for them to return more than 1 value, but I am concentrating on understanding how they work. For example, can someone help me with this little simple problem. Can someone explain how this works and why:
The C function is named printNumber and it has 3 arguments. The 1st argument should accept an integer number, the 2nd accept a floating point number, and the 3rd accept a double precision number. The body of the function should display the values of the data passed to the function when it is called. Write the main() function to call the printNumber() function. Use declaration statements to initialize the 3 numbers to be passed to the function. I will try to start and write what I know:

[c]
#include <stdio.h>

Void printNumber (int a, float b, double c);

int main ()
  #2  
Old 12-Mar-2004, 18:49
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Sorry...wait a second..I didnt mean to send this....I will complete the code
  #3  
Old 12-Mar-2004, 18:53
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
[c]
#include <stdio.h>

Void printNumber (int, float, double);

int main ()
{
int a;
float b;
double c;

a=3
  #4  
Old 12-Mar-2004, 19:01
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road

sorry


Sorry for the repeated code....I am hitting a certain button and it's sending the reply and I dont know which one I am hitting.

CPP / C++ / C Code:
#include <stdio.h>

Void printNumber (int, float, double);

int main ()
{
int a;
float b;
double c;
int show;


a=3;
b=6.5;
c=5.34;

show= printNumber(a,b,c);
printf("%d, %f, %f", a,b,c);
return 0;

Thanks a lot for the help AGAIN!! :-) Tommy

{{{Is this where the body of the function goes?}}}
  #5  
Old 12-Mar-2004, 19:24
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I'm not going to write your program for you, but I'll give you a boost with this:
CPP / C++ / C Code:
#include <stdio.h>

int AddNumbers (int, int);  // define the prototype for the function
                            // It returns the answer as an int
int main ()
{
    int a;    // define the variables
    int b;
    int c;

    a=3;    // set the values of the input variable
    b=8;

    c= AddNumber(a,b);  // call the function, return the sum
    printf("%d + %d = %d \n", a,b,c); 
    return 0;
}

// The function returns one int
// Two ints are passed in.  
// The value 'a' from main will enter the function as 'x'
// The value 'b' from will enter as 'y'
int AddNumbers (int x, int y)    // define the function 
{
    int ans;      // hold she sum of the parameters
    ans = x + y;  // calculate the answer
    return ans;   // return the sum
}
In the function I'm obviously adding the the values passed in. For your function, just change the contents accomplish you task.
  #6  
Old 13-Mar-2004, 00:14
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Thanks for the help Walter! It's starting to make sense the more I lookat the code. Are there any excellent tutorials out there that you know of that deals with functions? I have read a few tutorials, but there are probably a lot more out there. Are functions hard to grasp or is it me? I mean, do a lot of people have trouble learning functions? I dont know why it's taking me a long time to figure it out. I know functions are important and I need to learn them. do you have any suggestions in learning them? These questions are for whoever can help. Thanks......tommy
  #7  
Old 13-Mar-2004, 03:11
pan pan is offline
New Member
 
Join Date: Mar 2004
Location: Greece
Posts: 16
pan will become famous soon enough
Hey Tommy,

You may want to check this tutorial out:

http://www.cplusplus.com/doc/tutorial/tut2-2.html

One of the great advantages of functions is that they allow you to break your program down into sub-problems (this is called top-down design). This is advantageous because it allows you to work on individual problems without getting overwhelmed and confused with an entire problem at once. Maybe it would be helpful to think of functions as sub-programs, or mini-programs that perform a very specific task.

If you were having more problems with the syntax of a function and how it works, the tutorial link above should be able to help.

Regards,
Pan Thomakos
  #8  
Old 13-Mar-2004, 10:20
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,243
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by tommy69
Thanks for the help Walter! It's starting to make sense the more I lookat the code. Are there any excellent tutorials out there that you know of that deals with functions? I have read a few tutorials, but there are probably a lot more out there. Are functions hard to grasp or is it me? I mean, do a lot of people have trouble learning functions? I dont know why it's taking me a long time to figure it out.
I can't really answer these Q's. I don't think they are hard to grasp personally. But they aren't easy for everyone.
Quote:
Originally Posted by tommy69
I know functions are important and I need to learn them. do you have any suggestions in learning them? These questions are for whoever can help. Thanks......tommy
Write more of them. Write a couple programs that use them. For example, here's a main program you can add the functions to for practice. It's based on the one I wrote above:
CPP / C++ / C Code:
#include <stdio.h>
int main()
{
    float a = 45.0;
    float b =  5.;
    float ans;

    ans = functionAdd(a, b);
    printf(" %.2f + %.2f =  %.2f\n", a, b, ans);

    ans = functionSub(a, b);
    printf(" %.2f - %.2f =  %.2f\n", a, b, ans);

    ans = functionMult(a, b);
    printf(" %.2f * %.2f =  %.2f\n", a, b, ans);

    printf(" %.2f / %.2f =  %.2f\n", a, b, functionDivide(a, b));

    return 0;
}
Add the functions and their prototypes to this main. The divide function is the same as all the others. This is just another way to call it.
  #9  
Old 14-Mar-2004, 22:43
tommy69 tommy69 is offline
Junior Member
 
Join Date: Mar 2004
Posts: 40
tommy69 is on a distinguished road
Thanks a lot guys for the help. I really appreciate it. I hope I can get really good at writing functions because I have a big program that I need to write and it's a hard one and it involves a lot of functions. I seriously doubt I will be able to do it on my own. If I have trouble, then I will post the problem and see if I can get some help. I am not expecting anybody to write it for me because I won't learn that way. However, I might need someone to help start it for me or look over the code. You all have helped me a lot and it's obvious you know how to program. I took the Visual basic and advanced class and I know how to program a little in VB, but C is totally different. There are a lot of similarities and functions are one of them. I will keep in touch :-)

By the way, what does this mean. I need to write a C program using the getchar(), toupper(), and putchar() functions that echo back all letters entered in their uppercase form? The program should terminate if the suer types in either an S or a s. Does this mean that if a user types in any letter, except for the S and s, the uppercase form is displayed? Or does the user only type in an uppercase letter and the uppercase letter is displayed again? I am only trying to figure out what this program needs to do. I have never used these functions before, but I will experiment. I think I will use a Do/while loop with an If Else statement.

Take care guys and have a great week!! Tommy
  #10  
Old 14-Mar-2004, 23:36
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Well, as far as I know, the word terminate is a synonym for end. Since you can't go any further than the end, I'm thinking he wants the program to STOP once you read the letter S in upper or lower case. Why dont you just ask him?
__________________
-Aaron
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Pointers To Pointers in Functions elumira C Programming Language 8 05-Mar-2004 21:23
Calling functions within a function spudtheimpaler C Programming Language 5 02-Mar-2004 08:02
New to C...need help calling functions rbeierle C Programming Language 3 10-Feb-2004 18:46
Need C++ help with functions EvilIce C++ Forum 2 01-Dec-2003 16:04
need help to define a class in C++ yannoush C++ Forum 7 09-Sep-2003 00:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.