Thread: Creating a function in a proper way

  1. #1
    Registered User
    Join Date
    Nov 2014
    Posts
    18

    Creating a function in a proper way

    As I am taking my first steps in C, I study K&R (I guess most of you did the same, right?)

    In the introductory chapter about functions (1.7) there is an example showing how to create a function that is then called to calculate powers (b**n). I simplified it to calculate only one given power, 2**5:

    Code:
    #include <stdio.h>
    
    int power(int m, int n);
    main()
    {
    printf("%d", power(2,5));
    }
    
    int power(int base, int n)
    {
    int i, p;
    p = 1;
    for (i = 1; i <= n; ++i)
    p = p * base;
    return p;
    }
    which works fine, resulting 32.

    Now, trying to go deeper, I want to create a similar* program to make calculations with words and characters, but oh! characters look to me tougher than numbers all the time (*with "similar", I mean create a function and call it to do the job, instead of doing it in the body of the code).

    Lets say I want to make a program to scan a string of words and calculate the mean number of characters per word.
    The string will be "This is an extremely complicated code I would call it rocket science". It has 12 words, 57 chars (blanks don't belong to any word so they don't count) and 4.75 chars per word.

    My function, mean_num_chars, will have to
    (a) count chars without blanks
    (b) count words
    (c) calculate mean number of chars per word

    It will then be called to make the calculation in the above string.

    First things first: we already know how to use while(getchar!=EOF) to count characters (K&R chapter 1.5.2) but what if -instead- the input is a specific string? How to "read" it and how to tell my program that the string is finished? And most important: "reading" will be done in the function or in the rest of the body?

    Any hint on this before I proceed?
    Last edited by actuz; 11-11-2014 at 05:33 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'll give you some questions. If you work out the answers to them - check with any basic textbook if needed - you'll be on the path to answering your own question.

    What is an array?

    How is a string (like "Hello") is represented in memory?

    What is a pointer?

    When an array is passed as an argument to a function, what is passed?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a Function
    By JonathanS in forum C Programming
    Replies: 13
    Last Post: 09-18-2011, 01:22 AM
  2. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  3. Proper way to pass in 2d array to function?
    By stevenswj in forum C Programming
    Replies: 2
    Last Post: 12-06-2010, 11:02 PM
  4. proper function call syntax
    By jerrykelleyjr in forum C++ Programming
    Replies: 9
    Last Post: 12-27-2004, 08:43 PM
  5. Creating function
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 02-25-2002, 12:24 PM