Thread: how do i pass arguments to a function^^^please help

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    11

    Unhappy how do i pass arguments to a function^^^please help

    i need to write a program which opens a file in a function, not in main, and open it from dos command...

    ive writen the program to open it in main but im not aloud to do that i have to make a seperate function for opening a file

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h> 
    
    int main(int argc, char* argv[])
    {
    	
    	FILE * in;
    	int c;
    	int num;
    	int count = 0;
    	
    
    	in = fopen(argv[2],"r");
    	num = atoi(argv[1]);
    
    	while((c = fgetc(in)) != EOF) {
    
    		if(count < num) {
    			fputc(c, stdout);
    			count++;
    		}
    		else {
    			fprintf(stdout,"\n");
    			count = 0;
    
    		}
    
    	}
    
    	return 0;
    }
    my question is how do i go about passing the arguments to the function after declaring them in the main....

    thanks guys

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I see plenty of parameters being passed to plenty of functions already.
    Just write a function and call it in pretty much the same way as you're doing now.
    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.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    4
    you have already written a function main()... just do the same except name it something else and pass in something different inside the ().

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Code:
    MyFunction1(Param1, Param2);      // Calls MyFunction1() and passes-in variables Param1 & Param2
    A = MyFunction2(X, Y);     // Calls MyFunction2() and copies the return value into variable A
    Check-out the Function Tutorial. And, re-read the chapter in your book that introduces functions. You have a book right?

    Functions can be confusing because you will have to write it at least three times, in slightly different form... 1-the function protototype, 2- the function definition, 3-each time you call the function.

    Most functions require parameters. Some don't. Some functions return a value. Some don't.

    Take the time to make sure that you understand functions. Everything in C++ is done with functions!
    Last edited by DougDbug; 08-26-2004 at 10:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. commands and arguments problem
    By Martin Kovac in forum C Programming
    Replies: 2
    Last Post: 03-20-2009, 04:18 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  3. Replies: 3
    Last Post: 11-22-2007, 12:58 AM
  4. How can I pass arguments through a GUI (win32)?
    By bikr692002 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2006, 05:17 PM
  5. Parameter pass
    By Gades in forum C Programming
    Replies: 28
    Last Post: 11-20-2001, 02:08 PM