ページ 11

引数と戻り値について

Posted: 2011年6月02日(木) 18:38
by 流星
引数と戻り値について教えてください。
下のようなプログラムがあったとき引数と戻り値というのは、どれになるのでしょうか?
考え方も含めて分かる方、教えてください

コード:

関数の名前:add
引数:*head?
戻り値:わからないです
struct list *add( struct list *head )
{
	struct list *p, *new;
	char name_r[MAX_NAME_SIZE];
	char tel_r[MAX_TEL_SIZE];

	if(( new = ( struct list* ) malloc ( sizeof ( struct list ))) == NULL )
	{
		printf( "Memory error.\n" );
		free(new);
		return 0;
	}
	p=head;
	
	printf( "名前の入力:" );
	if( fgets(name_r,MAX_NAME_SIZE,stdin ) == NULL )
	{
		return 0;
	}
	if( strchr ( name_r,'\n' ) != NULL )
	{
		name_r[strlen(name_r)-1]='\0';
	}else
	{
		while( getchar() !='\n' );
	}
	
	printf( "電話番号の入力:" );
	if( fgets ( tel_r,MAX_TEL_SIZE,stdin ) == NULL )
	{
		return 0;
	}
	if( strchr ( tel_r,'\n' ) != NULL )
	{
		tel_r [ strlen ( tel_r ) -1 ] = '\0';
	}else
	{
		while(getchar() !='\n');
	}

	strcpy( new -> name,name_r );
	strcpy( new -> tel,tel_r );

	new -> next = head;
	head = new;

	return head;
}

Re: 引数と戻り値について

Posted: 2011年6月02日(木) 18:49
by a5ua
文法的には、
戻り値の型 関数名(引数リスト)
※引数リストは、型 名前[, 型 名前, ...]
です。

よって、
戻り値の型:「struct list *」
関数名:「add」
引数:「head」でその型は、「struct list *」
となります。

Re: 引数と戻り値について

Posted: 2011年6月02日(木) 21:41
by box
もっと単純な例で考えてみたらどうでしょうか。
下記のコードで、add関数の引数と戻り値は、それぞれ何ですか?

コード:

#include <stdio.h>

int add(int n)
{
    return n + 10;
}

int main(void)
{
    printf("%d\n", add(2));
    return 0;
}