Thread: Understanding functions

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    28

    Understanding functions

    I am trying to write a program that will remove vowels from an input string and output only consanants. I've looked all over for resources and coding examples that would point me in the right direction. Currently my function is written with a case statement, but as this is my first attempt at integrating a function into a program, I am missing something. Unfortunately, I dont know what that is.

    What I am thinking at the moment is I have my function built, and I currently coded a while statement to capture the text and output back to the screen. The problem is I have no idea how to properly have the function intercept each character and remove a vowel.

    Here is my code such as it is:

    Code:
    #include <stdio.h>
    
    int isvowel()
    {
    	//  Switch on the character
    
        switch (c) {
        
        //  Character is a vowel
        
            case 'A': case 'E': case 'I': 
            case 'O': case 'U': case 'Y':
    		case 'a': case 'e': case 'i':
    		case 'o': case 'u': case 'y':
                return true;
        
        //  Character is not a vowel
        
            default:
                return false;
    
    }
    
    int main(void)
    {
    	int c; // character variable integer initialized
    	printf("This program will strip all vowels from input text and print the output\n"); // printed messages
    	printf("Enter some text ;-)\n\n");
    	while ((c = getchar()) != EOF) { // while statement to capture characters
    	// my function goes here but I dont know yet how to code it properly 
    
    		printf("\n"); // prints out characters
    		putchar(c);
    		
    	
    }
    I think I should be using an if statement to seperate the vowels, but at this point, Im not sure how that is accomplished.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    To define a function you just use this format:

    return_type function_name(parameter_list)

    You call several functions in your program already. You just call your function the same way.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Code:
    int isvowel()
    {
    	//  Switch on the character
    
        switch (c) {
    Where did 'c' come from? Don't you want to pass a arguemtn to 'isvowel'? How else would it know what you want to test for a value?

    In your while loop you probably want to test to see if isvowel returned true or false right and then take an action depending on that?

    If these don't answer your question please ask a more direct and thought out question. Consult:
    http://www.catb.org/~esr/faqs/smart-questions.html
    For a reference on how to ask questions.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    10

    Thumbs up

    Asking folks to ask a smart question is deductively stupid. Asking somebody to ask a thorough question is more like what you want.
    Ask smart questions - if I would be that smart, I wouldn't need to ask questions.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    I apologize for my "stupiditiy" but Im very new to programming languages. Sometimes you dont know the right way to ask a question. I slept on my code and decided that I shouldnt tackle two concepts when I need to concentrate on one. I decided to re-do the code to use a compound if statement verses learning case and function in one program.

    My new code works perfectly. It just doesnt have a defined function which is the concept I am trying to learn. So I am reposting the working code and hope someone can take a few minutes to explain to a newbie how to shift the if statement into a defined function.

    Code:
    #include <stdio.h> //Include Files
    
    
    int main(void)  //Main Program
    {
    	int c, // i/o character initialized
    	int	n; // null character variable integer initialized
    	printf("This program will strip all vowels from input text and print the output\n"); // printed messages
    	printf("Enter some text ;-)\n\n");
    	while ((c = getchar()) != EOF) //{ // while statement to capture characters
    		if (c == 'a' || c == 'A'   //Compound if statement to capture vowels to null
    			|| c == 'e' || c == 'E'
    			|| c == 'i' || c == 'I'
    			|| c == 'o' || c == 'O'
    			|| c == 'u' || c == 'U'
    			|| c == 'y' || c == 'Y') 
    		n = c; //  captures vowels to a null character
    	//return 0;
    		else 
    		putchar(c); //prints non-vowels
    return 0;				
    }

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Asking folks to ask a smart question is deductively stupid.
    How so? Granted, if you ask a smart question, you're probably smart enough to go through the steps to answer your own question, but sometimes the problem is just too difficult at the time. One can be smart about asking questions and still ignorant about the topic, so your deduction is flawed.
    My best code is written with the delete key.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    What is it about defining a function you don't understand? You call functions all the time. putchar() is a function and you call it. main() is a function and you define it. Defining another function is exactly the same. I described in my previous post what the 3 different parts of a function definition are (the part before the actual body of the function anyway.)

    Your question needs to be more specific. Right now there's no way of answering you without actually doing it all for you. And we don't do homework for people here. We're here to answer specific questions that you're having trouble with.
    Last edited by itsme86; 08-31-2005 at 01:43 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    Quote Originally Posted by itsme86
    What is it about defining a function you don't understand? You call functions all the time. putchar() is a function and you call it. main() is a function and you define it. Defining another function is exactly the same. I described in my previous post what the 3 different parts of a function definition are (the part before the actual body of the function anyway.)

    Your question needs to be more specific. Right now there's no way of answering you without actually doing it all for you. And we don't do homework for people here. We're here to answer specific questions that you're having trouble with.

    I apologize. It looks like I was making this out to be harder than it was. But sometimes when you bang your head against the wall too many times, things look blurry after while. Im teaching myself and dont have an instructor to tell me where Im messing up. I took your advice and beat my compiler until it worked right. Something tells me it wont be the last time I have a silly question. I appreciate everyones advice and patience.

    Here is an excerpt of my code redone as a defined function:

    Code:
    int isvowel(int c)
    {	
    	int n; //initialize local 'null' character variable
    		if (c == 'a' || c == 'A'   //Compound if statement to capture vowels to null
    		|| c == 'e' || c == 'E'
    		|| c == 'i' || c == 'I'
    		|| c == 'o' || c == 'O'
    		|| c == 'u' || c == 'U'
    		|| c == 'y' || c == 'Y') 
    		n = c; //  captures vowels to a null character
    		else 
    			putchar(c); //puts non-vowels to variable c
    			return(c); //returns c to calling program	
    }

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Im teaching myself and dont have an instructor to tell me where Im messing up
    Good news! We'll be happy to step in and tell you where you're messing up so as to fill that void.

    >Something tells me it wont be the last time I have a silly question.
    If you don't have silly questions, you're most assuredly doing something wrong.

    >int isvowel(int c)
    This is a reserved name. Anything that starts with "is" and a lower case letter is reserved for future library extensions. You can fix it by saying:
    Code:
    int is_vowel ( int c )
    >//initialize local 'null' character variable
    That's a confusing comment because you don't use the variable as a 'null' character by any defintion of the term, and // style comments are only legal in C99.

    >putchar(c);
    I don't like this. Your function suggests that it tests a character and returns a boolean value consistent with the standard library. But what the function actually does is inconsistent because it has an unexpected side effect. I would expect something more like this:
    Code:
    int is_vowel ( int c )
    {
      c = tolower ( c );
      return c == 'a'
          || c == 'e'
          || c == 'i'
          || c == 'o'
          || c == 'u'
          || c == 'y';
    }
    My best code is written with the delete key.

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It looks like I was making this out to be harder than it was. But sometimes when you bang your head against the wall too many times, things look blurry after while. Im teaching myself and dont have an instructor to tell me where Im messing up. I took your advice and beat my compiler until it worked right.
    For sure. I'm glad you were able to figure it out. You'll be a pro in no time
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    Quote Originally Posted by Prelude

    >int isvowel(int c)
    This is a reserved name. Anything that starts with "is" and a lower case letter is reserved for future library extensions. You can fix it by saying:
    Code:
    int is_vowel ( int c )
    Code:
    Im using the book c by dissection and the exercise specifically asked you to write the function isvowel.  I do however understand IRL you would do it as you asked.
    >//initialize local 'null' character variable
    That's a confusing comment because you don't use the variable as a 'null' character by any defintion of the term, and // style comments are only legal in C99.
    Im using visual studio to write/compile and I happened to discover that commenting style. Its much quicker to use it. I know its not 100 percent legal ;-) The n variable is best described as a memory buffer. I needed n to give the if statement something to do with a vowel. As one of the persons suggested, my earlier simple program already had a function, I just didnt define it.

    >putchar(c);
    I don't like this. Your function suggests that it tests a character and returns a boolean value consistent with the standard library. But what the function actually does is inconsistent because it has an unexpected side effect. I would expect something more like this:
    Code:
    int is_vowel ( int c )
    {
      c = tolower ( c );
      return c == 'a'
          || c == 'e'
          || c == 'i'
          || c == 'o'
          || c == 'u'
          || c == 'y';
    }
    Here I dont follow you. I dont understand how the way you suggested would work in my program without major changes. How does 'tolower' work? I thought it was a function that would convert an uppercase letter to a lower one. As the function was supposed to take an input file and return an output file stripped of vowels, if there were capitals, it wouldnt remove them, right?

    The way I originally had the function coded, the putchar(c) wasnt in there. The function returned everything, not just non-vowels. Once I added that line into the function, everything worked. I realize my function would be best coded to return a boolerian t/f condition and print only if the condition returned true. Unfortunately, I spent a lot of time trying other things. I decided once I scrapped my original program, to modify my simple program with a non-defined function and see if I could fit the original code into a defined function. I ported the code and adjusted until it compiled, ran and worked. Once I get a some free time, Ill come back to this one and see if I can code it differently.

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    A function called isvowel() implies that it does nothing but test the character passed to it to see if it's a vowel or not. If it is it returns a true value, if it's not it returns a false value. However, your function prints non-vowels which seems like an odd "side effect". Using Prelude's function, you could write a loop in your main function with a body that looked something like:
    Code:
    {
      if(isvowel(c) == 0)
        putchar(c);
    }
    That would make sense to someone reading your code. If it's not a vowel, print it. Someone wouldn't be expecting the isvowel() function to actually be doing the printing, just testing the character to see if it really is a vowel.

    That's how all the ctype.h functions work also. isalpha() tells you if the character is alphabetic. It doesn't do anything like print the character if it is.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Aug 2005
    Posts
    28
    Thanks for clearing that up!

  14. #14
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    I do however understand IRL you would do it as you asked.
    Do you really see isvowel() making it into the standard library? I don't. Possible, yes. Probable, not really. And even if it does, it'll probably work the same way that Prelude wrote it--adding the requisite locale shtuff--, so you can just remove your version, recompile, and go have a pint.
    Im using visual studio to write/compile and I happened to discover that commenting style. Its much quicker to use it. I know its not 100 percent legal ;-)
    Use whatever works, my friend! If you know about portability issues, you can use your brain and ignore them when it's appropriate. Too many people go all anal retentive about stuff that really doesn't matter. I can't name a C compiler in common use that doesn't support BCPL style comments. 'Course, that doesn't keep me from always using C style comments in my C code. Maybe I'm a closet anal retentive.
    As the function was supposed to take an input file and return an output file stripped of vowels, if there were capitals, it wouldnt remove them, right?
    Sure it would. Prelude's version is case insensitive, so it would return true for both 'A' and 'a'. That's because c is converted to lower case so that the tests aren't duplicated. If he didn't do that then all of the tests would have to mirror the upper case:
    Code:
    int isvowel(int c) {
      return c == 'a' || c == 'A'
          || c == 'e' || c == 'E'
          || c == 'i' || c == 'I'
          || c == 'o' || c == 'O'
          || c == 'u' || c == 'U'
          || c == 'y' || c == 'Y';
    }
    They both do the same thing, but Prelude's is more elegant.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Functions are Still Not Understood.
    By errigour in forum C Programming
    Replies: 6
    Last Post: 04-09-2009, 02:54 PM
  3. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  4. Help Understanding Passing Arrays To Functions
    By jrahhali in forum C++ Programming
    Replies: 7
    Last Post: 04-10-2004, 02:57 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM